Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    focus after a submit button

    hi,

    I have a submit button in a web form. I want to write some javascript such that: after I submit the form, an alert window pop out, after I click ok, it focus on an input text field.

    the problem is, after the submit button, the page refrsh and it won't focus on it any more.

    could you please tell me how to solve this problem?



    <html>
    <head>

    <script type="text/javascript">
    function getfocus()
    {
    alert("hello")
    document.getElementById('myAnchor').focus();
    }

    function losefocus()
    {
    document.getElementById('myAnchor').blur();
    }
    </script>
    </head>

    <body>
    <a id="myAnchor2" href="http://www.w3schools.com">Visit W3Schools.com</a>
    <input type="text" id="myAnchor" />
    <br /><br/>
    <form>
    <input type="submit" onclick="getfocus()" value="Get focus">
    <input type="button" onclick="losefocus()" value="Lose focus">
    </form>


    </body>

    </html>

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,471
    Thanks
    0
    Thanked 634 Times in 624 Posts
    You should replace the onclick in the submit button with an onsubmit attached to the form tag itself - you can then return false to prevent the form being submitted which will prevent the page reloading.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you. could you help me modify the code and let me see !!!

  • #4
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    what syntax will help me stop the form being submitted

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,471
    Thanks
    0
    Thanked 634 Times in 624 Posts
    <form onsubmit="return getfocus()">

    and then add return false to the end of the function

    (and get rid of the onclick from the submit button).
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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