Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format a text in the textbox

Tags:

javascript

I am trying to format a selected text to bold inside a textbox but not happening. However, it is hapenning in a span/div. My code

<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')"> 
<span id="spOutput"></span> 
</body> 
</html> 

Is it possible to do it insde a textbox. If so how?

Thanks

like image 1000
lightswitchlover Avatar asked Mar 16 '26 07:03

lightswitchlover


1 Answers

No, it's not possible to style the text in a text box. You can make your div or span contentEditable=true if you need formatted text that is editable.

like image 126
Andy Ray Avatar answered Mar 17 '26 21:03

Andy Ray



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!