onChange event and focus()

Discussion in 'Javascript' started by SimonFx, Feb 1, 2005.

  1. SimonFx

    SimonFx Guest

    Am I right in assuming that you can NOT set focus() back to a field in
    its onChange event? Is there another way - I have validation that I
    would like to set the focus back to the invalid field onChange?

    Eg:
    <input type=text" name="tstfld" onChange="window.alert('Changed!');
    this.focus()">
     
    SimonFx, Feb 1, 2005
    #1
    1. Advertisements

  2. SimonFx

    SimonFx Guest

    I think I have solved this using setTimeout, but it feels dirty.

    function validateField (f){
    // blah blah field is bad so:
    setTimeout ('document.' + f.form.name + '.' + f.name + '.focus()',100);
    setTimeout ('window.alert (\'Invalid entry.\')', 100);
    }

    </SCRIPT>
    </HEAD>
    <BODY>

    <FORM NAME="myForm">
    <input type="text" name="myFld" onChange="validateField(this);">
    </FORM>
     
    SimonFx, Feb 1, 2005
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.