944,084 Members | Top Members by Rank

Ad:
Apr 8th, 2005
0

Add textbox to editable div problem

Expand Post »
I am having a problem with a javascript script.

Add this code to an html file if you wanna try it.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <script>
  4. var n = 0;
  5. function addtb(){
  6. document.getElementById('TextEditor').focus();
  7. cmd = 'InsertInputText';
  8. document.getElementById('TextEditor').document.execCommand(cmd,'true');
  9. document.getElementById('TextEditor').blur();
  10.  
  11.  
  12. var TE = document.getElementById('TextEditor');
  13. var TEcn=TE.children;
  14.  
  15. for (i=0;i<TEcn.length;i++)
  16. {
  17. n++;
  18. if(TEcn[i].id == "")
  19. TEcn[i].id = "tb" + n;
  20.  
  21. }
  22. }
  23. function tellids(){
  24. var TE = document.getElementById('TextEditor');
  25. var TEcn=TE.children;
  26.  
  27. for (i=0;i<TEcn.length;i++)
  28. {
  29. alert(TEcn[i].id);
  30.  
  31. }
  32. }
  33. </script>
  34. </head>
  35. <body bgcolor=dddddd>
  36. <div id="TextEditor" contenteditable="true" indicateeditable="true"
  37. style="BACKGROUND-COLOR:white;OVERFLOW: auto;WIDTH: 600px;HEIGHT: 500px;WORD-WRAP: break-word"></div>
  38. <input type=button onclick="addtb();" value="add a text box">
  39. <input type=button onclick="tellids();" value="alerts of textbox ids">
  40. <br>
  41.  
  42. After you add your first textbox to the editor, you need to unselect it to be able to add another textbox with only one click of the button.
  43. If you dont, you will have to doubleclick the button, and I dont want that.
  44. I would like the selected textbox to be blurred and then add another textbox with a single click of the button.
  45.  
  46. </body>
  47. </html>
After you add your first textbox to the editor, you need to unselect it to be able to add another textbox with only one click of the button.
If you dont, you will have to doubleclick the button, and I dont want that.
I would like the selected textbox to be blurred and then add another textbox with a single click of the button.

If anyone could help, I would really appreciate it.

thx
Similar Threads
Reputation Points: 19
Solved Threads: 2
Posting Shark
belama is offline Offline
962 posts
since Mar 2005
Apr 8th, 2005
1

Re: Add textbox to editable div problem

You need to empty the current selection range.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function addtb()
  2. {
  3. document.selection.empty();
  4. document.getElementById("TextEditor").focus();
  5. cmd = 'InsertInputText';
  6. document.getElementById('TextEditor').document.execCommand(cmd,'true');
  7.  
  8. var TE = document.getElementById('TextEditor');
  9. var TEcn=TE.children;
  10.  
  11. for (i=0;i<TEcn.length;i++)
  12. {
  13. n++;
  14. if(TEcn[i].id == "")
  15. TEcn[i].id = "tb" + n;
  16. }
  17.  
  18. }
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
Apr 8th, 2005
0

Re: Add textbox to editable div problem

Thanks. So simple and it works.
Reputation Points: 19
Solved Threads: 2
Posting Shark
belama is offline Offline
962 posts
since Mar 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Making web site fill browser screen
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Sort Tables...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC