| | |
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!!!!!; 21 Days Ago at 5:08 am.
0
#2 20 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 18 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 17 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 17 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!!!!!; 17 Days Ago at 9:20 am.
0
#9 17 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 |
ajax ajaxcode ajaxexample ajaxhelp ajaxjspservlets animate automatically beta box browser bug calendar captchaformproblem checkbox child class close column createrange() css cursor date debugger dependent disablefirebug dom download dropdown editor element embed engine error events explorer ext file form forms getselection google gwt gxt hiddenvalue highlightedword hint html htmlform ie7 ie8 iframe images internet java javascript javascripthelp2020 jawascriptruntimeerror jquery jsf jsfile jump libcurl math media microsoft mimic object onmouseoutdivproblem onreadystatechange parent paypal pdf php player position post problem programming progressbar regex runtime scroll search security select shopping size software sql text textarea unicode w3c web website window windowofwords windowsxp wysiwyg \n





