Textarea Issue

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Luke Warm
Forum Newbie
Posts: 11
Joined: Fri Nov 13, 2009 9:42 am

Textarea Issue

Post by Luke Warm »

Hi,

I'm creating a text to bash script generator and I'm having trouble trying to enter BASH tags into a textarea.

Here's my code form the HTML page:

Code: Select all

<html>
<head>
<title>BASHBuilder</title>
</head>

<script type="text/javascript"> 
<!-- 
    function formatText(el,tagstart,tagend) {
  	if (el.setSelectionRange) {
 		el.value = el.value.substring(0,el.selectionStart) + tagstart + el.value.substring(el.selectionStart,el.selectionEnd) + tagend + el.value.substring(el.selectionEnd,el.value.length)
  	}
  	else {
  		// IE code here...
  	}
  }

function preview() {   msg=open("","DisplayWindow","scrollbars=1,resizable=yes,toolbar=1,directories=no,menubar=yes,width=800,height=600,left=240,top=80");
   msg.document.write('<pre>',form1.text1.value,'</pre>');
}
//--> 
</script> 
<body onload="document.form1.text1.focus();">
<center>
<form name="form1">
<textarea id="text1" rows="12" cols="60" wrap="virtual"></textarea>
<br/><input type="button" value="Convert!" onclick="document.form1.text1.select();formatText(document.getElementById('text1'),'echo "','"');"/>&nbsp;&nbsp;<input type="reset" value="Clear" onclick="document.form1.text1.focus()" />&nbsp;&nbsp;<INPUT TYPE="button" VALUE="Preview" onClick="preview(this)" class=button>
</center>
</body>
</html>
I want to be able to place the "echo" tag before each line in the texarea but haven't been able to find any way to do it. Any suggestions?
I'd be willing to resort to writing it in PHP if necessary especially if it would be easier.

Any help is appreciated...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Textarea Issue

Post by John Cartwright »

I'm not very familiar with bash, so I suspect you will probably need to incorporate some form of quote escaping.. but otherwise, the following should do

Code: Select all

<html>
<head>
<title>BASHBuilder</title>
</head>

<script type="text/javascript">
    function convertToBash(id)
    {
        var input = document.getElementById(id);
        input.value = 'echo "'+ input.value.split("\n").join('"\necho "') +'"';
    }
</script>
<body>
<center>
<form name="form1">
    <textarea id="text1" rows="12" cols="60"></textarea><br/>
    <input type="button" value="Convert!" onclick="convertToBash('text1');"/>
</form>
</center>
</body>
</html>
Luke Warm
Forum Newbie
Posts: 11
Joined: Fri Nov 13, 2009 9:42 am

Re: Textarea Issue

Post by Luke Warm »

That did it!!

Do you know if it's possible to copy and paste to the textarea using javascript (using a button) in Linux?
Post Reply