SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Focus by ID - what is wrong?

    my form code is
    HTML Code:
    <form name="form_case_new" method="post" action="/case/start.php" id="form_case_new">
    	<fieldset>
    		<legend>Case information</legend>
    
    		<label for="name">Case Name:</label>
    		<input type="text" name="name" value="" class="text" id="focus_textbox" />
    		<br />
    		<label for="details">Details:</label>
    		<textarea rows="5" cols="50" name="details" class="text"></textarea>
    		<br />
    	</fieldset>
    
    	<fieldset>
    		<legend>Submit case information</legend>
    		<input type="submit" name="case_new" value="Start New Case" class="button" />
    
    	</fieldset>
    </form>
    my javascript is
    [javascript]
    document.getElementById("focus_textbox").focus();
    [/javascript]

    what am i doing wrong?
    ---------------------------
    Errors = Improved Programming.
    My Site

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try using setTimeout() with focus() to set focus

    Code:
    setTimeout('document.getElementById("focus_textbox").focus()',1);
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  3. #3
    Founder of Primal Skill Ltd. feketegy's Avatar
    Join Date
    Aug 2006
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try to put the javascript in the windows onload event. maybe the element it's not rendered yet, but the javascript tries to focus it.
    If you put to the windows onload event the javascript fires only when the elements are rendered.

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,869
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    It will work provided that the tag with that id has already been output to the browser. Either wait for the whole page to load using window.onload or use setTimeout to continually test if it has loaded before running the code - see http://javascript.about.com/library/blfastdom.htm for the code to do this.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. That was the problem. I put it in onLoad and it worked.

    Quite a catch for people like me.
    ---------------------------
    Errors = Improved Programming.
    My Site

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,869
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    One of the most common DOM errors - trying to access parts of the web page before they have loaded.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •