onblur call issue

Discussion in 'Javascript' started by Mahesh, May 2, 2007.

  1. Mahesh

    Mahesh Guest

    Hi all,

    Can any one advice what can be a work around here?
    When i run the code below, its showing the alert msg twice onfocus out
    of tf1

    Thanks in advance,
    Maheshkumar

    <html>
    <script type="text/javascript">

    function setFocus(ref) {
    globalTakeFocus=ref;
    setTimeout("globalTakeFocus.focus();globalTakeFocus.select()",100);
    }

    function checkfun()
    {
    if(document.getElementById("tf1").value=="")
    {
    //document.getElementById("tf1").focus();
    alert("The field is empty");
    setFocus(document.getElementById("tf1"));
    return;
    }

    if(document.getElementById("tf2").value=="")
    {
    //document.getElementById("tf2").focus();
    alert("The field is empty");
    setFocus(document.getElementById("tf2"));
    return;
    }
    }

    </script>
    <body>
    <table>
    <form>
    <tr> <td>
    TF1: <input id="tf1" type="text" value="" onblur="checkfun();"/></td>
    </tr>
    <tr><td>
    TF2: <input id="tf2" type="text" value="tf2" onblur="checkfun()"/></
    td>
    </tr>
    </form>
    </table>
    </body>
    </html>
     
    Mahesh, May 2, 2007
    #1
    1. Advertisements

  2. Mahesh

    marss Guest

    Sequence of the events:
    1.tf1 lost focus.
    2.tf2 got focus.
    3.alert that was caused by tf1 onblur event is shown up (tf2 lost
    focus).
    4.alert that was caused by tf2 onblur event is shown up.
    5.tf1 got focus.

    You can avoid it in this way:

    function checkfun(sender)
    {
    if(sender.value=="")
    {
    alert("The field is empty");
    setFocus(sender);
    }
    }

    TF1: <input id="tf1" type="text" value="" onblur="checkfun(this)"/>
    TF2: <input id="tf2" type="text" value="tf2" onblur="checkfun(this)"/>

    Regards
     
    marss, May 3, 2007
    #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.