dcsimg
www.webdeveloper.com
Results 1 to 3 of 3

Thread: focus()

  1. #1
    Join Date
    May 2007
    Location
    London, United Kingdom
    Posts
    430

    focus()

    Sitting here doing my JavaScript Fundamentals studies and can't figure out this little focus() problem...

    Take a look, and could you please tell me why it does not focus() back onto the input?

    HTML Code:
    function nameCheck(form)
    { var myValue = form.name.value;
    if(!(myValue))
    { alert("You forgot your name");
    form.name.focus();
    return;}
    }
    
    <form action="" method="post" name="enquiry" class="form">
    <table width="100%" border="0" cellpadding="5" cellspacing="0">
    <tr><td width="150" align="right"><label>Name:</label></td>
    <td width="25">&nbsp;</td>
    <td><input name="name" type="text" size="32" maxlength="32" onblur="nameCheck(this.value);" /></td></tr>
    Basically, when a user clicks in the name input box, then clicks out of it, it should display that a name was not entered and focus() back into the input box... But it is not doing that. (please note this is just a snippet of a bigger form with more input boxes)

    Thanks a mill in advance...

  2. #2
    Join Date
    Jun 2007
    Posts
    667
    Quote Originally Posted by Hooded_Villian View Post
    Basically, when a user clicks in the name input box, then clicks out of it, it should display that a name was not entered and focus() back into the input box... But it is not doing that.
    It is, but the focus is being taken by the alert's OK button which is still down at that point.
    Use a timeout to set the focus or better still don't alert on blur, it's not a good practice.

  3. #3
    Join Date
    May 2007
    Location
    London, United Kingdom
    Posts
    430
    Quote Originally Posted by Sterling Isfine View Post
    It is, but the focus is being taken by the alert's OK button which is still down at that point.
    Use a timeout to set the focus or better still don't alert on blur, it's not a good practice.
    Ooook... Fair enough. I was just playing around with it, to get the hang of it all.

    To be fair, the example used in my study manual only did the alert() and focus() after a submit onclick() was activated...

    So, I usually do form validation with PHP. (I know, I've done it backwards. Started studying PHP before JS. Haha) What would be the best way to output to the user, when using JS for validation? alert()? Or is there a way to output to the browser?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles