| | |
Set the cursor focus on the dynamic text box created
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Mar 2009
Posts: 5
Reputation:
Solved Threads: 0
Hi there,
I'm having a little trouble in 'Setting the cursor focus' on a text box, that I'm creating dynamically, when Clicked on a button (having the onccick attribute).
Detail:
I have this button
And this generateRow() javascript function.
So everytime the button is clicked, I get 3 text boxes. First text box, and the last text box, I made them uneditable.
What I wanted to do was, setting the cursor focus in the second text box.
I tried putting,
inside the generateRow function, so as to grab the dynamic variable, and make that field to be focused (and therby setting the cursor there).
but this always comes up with the javascript error, saying its NULL.
Does anybody know a solution for this ? Please let me know if you have one.
Thanks
Kalpsen
I'm having a little trouble in 'Setting the cursor focus' on a text box, that I'm creating dynamically, when Clicked on a button (having the onccick attribute).
Detail:
I have this button
html Syntax (Toggle Plain Text)
<INPUT TYPE="button" name="add_ans" STYLE="background-color:#CCFF00;width:50;height:30;" VALUE="Add Answer" onclick="generateRow()">
And this generateRow() javascript function.
JavaScript Syntax (Toggle Plain Text)
var answer_count = 0; function generateRow() { answer_count++; var order = 'order'+answer_count; var answer_text = 'answer_text'+answer_count; var score = 'score'+answer_count; var d=document.getElementById("answer_header"); d.innerHTML+="<table border=0 cellspacing=1 cellpadding=1 width=630><tr><td><input type='text' name='"+order+"' size='6' onfocus='this.blur();' value='0'></td><td><input type='text' name='"+answer_text+"' size='70'></td><td><input type='text' name='"+score+"' size='6' onfocus='this.blur();' value='0'></td></tr></table>"; }
So everytime the button is clicked, I get 3 text boxes. First text box, and the last text box, I made them uneditable.
What I wanted to do was, setting the cursor focus in the second text box.
I tried putting,
JavaScript Syntax (Toggle Plain Text)
var txtBox=document.getElementById('answer_text'+answer_count); if (txtBox!=null ) txtBox.focus();
but this always comes up with the javascript error, saying its NULL.
Does anybody know a solution for this ? Please let me know if you have one.
Thanks
Kalpsen
Last edited by peter_budo; Mar 6th, 2009 at 8:42 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
•
•
Join Date: Mar 2009
Posts: 1
Reputation:
Solved Threads: 0
Hi there,
All you need to do is give your second input an ID, and then set focus to that element, see line #9.
Hope that helps!
All you need to do is give your second input an ID, and then set focus to that element, see line #9.
javascript Syntax (Toggle Plain Text)
var answer_count = 0; function generateRow() { answer_count++; var order = 'order'+answer_count; var answer_text = 'answer_text'+answer_count; var score = 'score'+answer_count; var d=document.getElementById("answer_header"); d.innerHTML+="<table border=0 cellspacing=1 cellpadding=1 width=630><tr><td><input type='text' name='"+order+"' size='6' onfocus='this.blur();' value='0'></td><td><input type='text' name='"+answer_text+"' id='"+answer_text+"' size='70'></td><td><input type='text' name='"+score+"' size='6' onfocus='this.blur();' value='0'></td></tr></table>"; document.getElementById(answer_text).focus(); }
Hope that helps!
![]() |
Similar Threads
- Help with automatic update problem and more (Viruses, Spyware and other Nasties)
- Cannot find server or DNS Error - please help! (Viruses, Spyware and other Nasties)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: is it possible to retrieve manufacturer information using javascript?
- Next Thread: gallery wish
| Thread Tools | Search this Thread |
acid2 ajax ajaxcode ajaxhelp animate automatically beta box bug calendar captcha cart checkbox child class column cookies createrange() css cursor decimal design dom download dropdown editor element engine enter error events explorer file focus form forms frameworks google gwt html htmlform ie8 iframe image() images index internet java javascript jawascriptruntimeerror jquery jsf jsfile jump listbox math matrixcaptcha menu microsoft mimic mp4 object onmouseoutdivproblem onmouseover onreadystatechange parent pdf php player post problem progressbar prototype rated rating regex runtime scale scroll search select session shopping size sql star starrating stars text textarea validation w3c web website window windowofwords windowsxp wysiwyg xml \n





