View Full Version : javascript textarea

01-30-2011, 08:07 AM

i am new to javascript. i want to make a form where a textarea, some buttons like bold,italic,link etc will be availablw. when user selecet some text from textarea and click bold button then one openig and closing tag will be added like this


how can i do this using javascript. any idea?
pls. help

low tech
01-30-2011, 08:40 AM

like this?


click view source to see opening and closing tags


Philip M
01-30-2011, 09:17 AM
Double posted at
http://www.codingforums.com/showthread.php?t=216798 :(


<script type="text/javascript">

// from http://www.codingforums.com/showthread.php?t=134113 - Author Kor

function formatText(el,tag){
var selectedText = document.selection?document.selection.createRange().text:el.value.substring(el.selectionStart,el.sel ectionEnd);// IE:Moz
if (selectedText == "") {return false}
var newText='<'+tag+'>'+selectedText+'</'+tag+'>';
if(document.selection){ //IE
else{ //Moz
el.value=el.value.substring(0,el.selectionStart)+newText+el.value.substring(el.selectionEnd,el.value .length);

<form name="myForm">
<textarea name="myTextarea" style="font-family:arial; font-size = 12pt" rows = "12" cols = "50">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</textarea><br>

<input type="button" value="Bold" onclick="formatText (myTextarea,'b');" />
<input type="button" value="Italic" onclick="formatText (myTextarea,'i');" />
<input type="button" value="Underline" onclick="formatText (myTextarea,'u');" />

This works with all browsers. Author Kor.

"Remember that there is nothing stable in human affairs; therefore avoid undue elation in prosperity, or undue depression in adversity". Socrates - Greek philosopher in Athens (469 BC - 399 BC)

01-31-2011, 04:43 AM

your tutorial is very helpful to me. thank you very much.

in your tutorial when i click view source button then html code is showing. how can i get this value (html code) in a php variable..pls help me...