PDA

View Full Version : Select text without current value



tech_support
06-19-2006, 09:39 AM
Hello,

I was wondering if you can do a script where when you type in a textarea and click on a button it has the value plus something else and minus the current value.

For example I'm typing text and then i want a <b> code around it replacing the original.

I want this:

<b>text text text</b>

Not this:

text text text<b>text text text</b>


Here's what I'm doing right now



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<script>
function insert(tag,endtag) {

var text = ""
var currentValue = document.form1.html.value
text = (document.all) ? document.selection.createRange().text : document.getSelection();
document.form1.html.value += tag + text + endtag
return true;
}
</script>
</head>

<body>
<div class="mainbody">
<form id="form1" name="form1" method="post" action="">
<textarea name="html" cols="100" rows="15">Type here...</textarea>
<input name="bold" type="button" id="bold" value="Bold" onclick="insert('<b>','</b>')"/>
</form>

</div>
</body>

</html>

Vallim
06-19-2006, 12:09 PM
Hi,

You could use the following code as below , to get the bold tag been placed inbetween the text of the TextArea.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<script>
function Bold()
{
var formData = document.selection.createRange().text;
if (formData != '')
{
document.selection.createRange().text = '<b>'+formData+'</b>';
}
else
{
alert("Select the data to be get bold");
}
}
</script>
</head> <body> <div class="mainbody">
<form id="form1" name="form1" method="post" action="">
<textarea name="html" cols="100" rows="15">Type here...</textarea>
<input name="bold" type="button" id="bold" value="Bold" onclick="Bold()" />
</form>
</div>
</body>
</html>


Let me know if u have any doubts.

Regards,
Valli

tech_support
06-20-2006, 05:47 AM
Thanks! Now i'll just edit that....