| | |
add text onclick to textarea where carot is
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Apr 2009
Posts: 281
Reputation:
Solved Threads: 2
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<script> function addText(event){ document.getElementById("insertid").value += (event.srcElement || event.target).firstChild.nodeValue.toString(); } </script> <textarea id="inserid"></textarea> <table onclick=addText(event)><tr><td>[bbcode][/bbcode]</td><td>[bbcode2][/bbcode2]</td></tr></table>
like if a user entered in the textearea:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
hey whats up
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
hey[bbcode][/bbcode] whats up
how do i do that?
Last edited by SKANK!!!!!; 26 Days Ago at 5:08 am.
0
#2 26 Days Ago
This is because you are appending the new value always. You have to get the position of the carrot and do some string operation. here is a function to insert in between:-
(http://alexking.org/blog/2003/06/02/...ing-javascript)
(http://alexking.org/blog/2003/06/02/...ing-javascript)
javascript Syntax (Toggle Plain Text)
function insertAtCursor(myField, myValue) { //IE support if (document.selection) { myField.focus(); sel = document.selection.createRange(); sel.text = myValue; } //MOZILLA/NETSCAPE support else if (myField.selectionStart || myField.selectionStart == '0') { var startPos = myField.selectionStart; var endPos = myField.selectionEnd; myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length); } else { myField.value += myValue; } } // calling the function insertAtCursor(document.formName.fieldName, 'this value');
When you think you have done a lot, then be ready for YOUR downfall.
0
#4 24 Days Ago
here:
html Syntax (Toggle Plain Text)
<script> function addText(event){ //document.getElementById("insertid").value += //(event.srcElement || event.target).firstChild.nodeValue.toString(); insertAtCursor(document.getElementById("insertid"), (event.srcElement || event.target).firstChild.nodeValue.toString()) } </script> <textarea id="inserid"></textarea> <table onclick=addText(event)><tr><td>[bbcode][/bbcode]</td><td>[bbcode2][/bbcode2]</td></tr></table>
When you think you have done a lot, then be ready for YOUR downfall.
•
•
Join Date: Apr 2009
Posts: 281
Reputation:
Solved Threads: 2
0
#5 23 Days Ago
•
•
•
•
here:
html Syntax (Toggle Plain Text)
<script> function addText(event){ //document.getElementById("insertid").value += //(event.srcElement || event.target).firstChild.nodeValue.toString(); insertAtCursor(document.getElementById("insertid"), (event.srcElement || event.target).firstChild.nodeValue.toString()) } </script> <textarea id="inserid"></textarea> <table onclick=addText(event)><tr><td>[bbcode][/bbcode]</td><td>[bbcode2][/bbcode2]</td></tr></table>
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<script> function addText(event){ //document.getElementById("insertid").value += //(event.srcElement || event.target).firstChild.nodeValue.toString(); insertAtCursor(document.getElementById("insertid"), (event.srcElement || event.target).firstChild.nodeValue.toString()) } </script> <textarea id="insertid"></textarea> <table onclick=addText(event)><tr><td>[bbcode][/bbcode]</td><td>[bbcode2][/bbcode2]</td></tr></table>
•
•
Join Date: Apr 2009
Posts: 281
Reputation:
Solved Threads: 2
0
#8 23 Days Ago
how am i supposed to call it? on hover or something?
well i treid
well i treid
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
onclick="addText()" and it still didnt work
Last edited by SKANK!!!!!; 23 Days Ago at 9:20 am.
0
#9 23 Days Ago
html Syntax (Toggle Plain Text)
<table id='my_table'><tr><td>[bbcode][/bbcode]</td><td>[bbcode2][/bbcode2]</td></tr></table>
Now on body on load bind the 'onclick' event this way
The body html:
html Syntax (Toggle Plain Text)
<script> function init() { var table = document.getElementById('my_table'); if(table) { table.onclick = function(e) { // Now call your addText function addText(e); } } } </script> <body onload='init();'></body>
Hope you got all that.
Note there are better way to bind events please have a look at 'addEventListener()' javascript function
When you think you have done a lot, then be ready for YOUR downfall.
![]() |
Similar Threads
- Add Text To multiline textbox (C++)
- Add/Edit/Delete From Input to text area (JavaScript / DHTML / AJAX)
- how to add the text in text boxes. (ASP.NET)
- Add text to existing text file (C#)
- add text box on click (JavaScript / DHTML / AJAX)
- add text to images (JavaScript / DHTML / AJAX)
- code illiterate - add text from 5 TEdit to 1 (Pascal and Delphi)
- Custom input tag? (JavaScript / DHTML / AJAX)
- can't concatenate variables and add text (ASP.NET)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: is it possible to over write internal java script?
- Next Thread: Need help building an instant messaging system with AJAX/PHP
| Thread Tools | Search this Thread |
acid2 ajax ajaxcode ajaxhelp animate array automatically beta box bug calendar cart checkbox class codes column cookies createrange() css cursor date debugger decimal design dom download dropdown editor element enter error events explorer firefox focus form frameworks getselection google gwt html htmlform ie8 iframe image() images index internet java javascript jawascriptruntimeerror jquery jsf jsfile jsp jump listbox maps masterpage math menu microsoft mimic mp4 object onmouseover onreadystatechange parent php player post problem programming progressbar prototype redirect regex runtime safari scale scriptlets search select session shopping size sql text textarea toggle validation variables w3c web website window windowofwords windowsxp wysiwyg \n





