How do i format the perticular selected word in the text box to bold or italic on click of button.

Posted by Mahendrabasutkar under JavaScript on 9/9/2011 | Points: 10 | Views : 1745 | Status : [Member] | Replies : 4
Hi All,

How do i format the perticular selected word in the text box to bold or italic on click of button.

Thanks,
mahendrabasutkar




Responses

Posted by: Niladri.biswas on: 9/9/2011 [Member] Platinum | Points: 25

Up
0
Down
As per my knowledge TextBox displays only plain-text. So u can try with a div/span. OR think of any other alternative. Here is an example

<html>
<head>
<title>Untitled Document</title>
<script type="text/javascript">
function FormatSelectedWord(txtBox,style)
{

var selectedText = document.selection
? document.selection.createRange().text
: txtBox.value.substring(txtBox.selectionStart,txtBox.selectionEnd);

if(selectedText!='')
{
var newText='<'+style+'>'+selectedText+'</'+style+'>';
txtBox.value=txtBox.value.replace(selectedText,newText)
spOutput.innerHTML=txtBox.value;
}
}
</script>
</head>
<body>


<input type="text" name="txtBox"> </input>
<input type="button" value="Bold" onclick="FormatSelectedWord(txtBox,'b')">
<input type="button" value="Italic" onclick="FormatSelectedWord(txtBox,'i')">
<span id="spOutput"></span>
</body>
</html>

Hope this will help


Best Regards,
Niladri Biswas

Mahendrabasutkar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mahendrabasutkar on: 9/9/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Niladri,

The code what you have given is working as per the requirment, but the result has to apper with in the same text box.
please view and please let me know.

Thanks&Regards
mahendrabasutkar

Mahendrabasutkar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Niladri.biswas on: 9/9/2011 [Member] Platinum | Points: 25

Up
0
Down
That's what i said... it canot be done inside a textbox because it displays only plain-text

Best Regards,
Niladri Biswas

Mahendrabasutkar, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mahendrabasutkar on: 9/10/2011 [Member] Starter | Points: 25

Up
0
Down
Thanks Niladri...

Regards,
mahendra

Mahendrabasutkar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response