| | |
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
![]() |
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!!!!!; Nov 8th, 2009 at 5:08 am.
0
#2 Nov 8th, 2009
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 Nov 10th, 2009
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.
0
#5 Nov 11th, 2009
•
•
•
•
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>
0
#8 Nov 11th, 2009
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!!!!!; Nov 11th, 2009 at 9:20 am.
0
#9 Nov 11th, 2009
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 |
Tag cloud for JavaScript / DHTML / AJAX
ajax ajaxexample ajaxjspservlets array autoplay blackjack browser captchaformproblem checkbox child class close codes date debugger dependent developer disablefirebug dom editor element embed engine events explorer ext file firefox flash form forms game gears getselection google gxt hiddenvalue highlightedword hint html ie7 ie8 iframe internet java javascript javascripthelp2020 jquery jsf jsfile jsp jump libcurl maps marquee masterpage math matrixcaptcha media mysql object onerror onmouseoutdivproblem onreadystatechange parent passing paypal pdf php player position post programming rated redirect runtime safari scriptlets scroll search security session shopping size software solutions star stars stretch synchronous toggle tweet unicode variables web webkit webservice window wysiwyg \n





