SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Dec 2007
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Javascript alert problem!

    Hey there guys

    I have a simple form script with radio buttons that when i hit the submit button and "robot" is selected, it returns a pop up(alert) saying that it dosn't allow robots!!
    But the thing is that it returns TWO alerts????don't know why?

    Please help! here is my script:



    HTML Code:
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    <script type="text/javascript">
    function submitted() {
    var robot = document.getElementById('one');
    var male = document.getElementById('two');
    var female = document.getElementById('three');
    if(robot.checked == true){
    alert("Go Away robot");
    return false;
    }
    
    }
    </script>
    </head>
    
    <body>
    <form name="form1" method="post" action="index.html" onsubmit="return submitted()">
      <p>
        <input  id="one" name="radiobutton" type="radio" value="robot" checked="checked" />
      robot
      <input id="two" name="radiobutton" type="radio" value="male" />
      male
      <input id="three" name="radiobutton" type="radio" value="female" />
      female</p>
      
      <input type="submit" name="Submit" value="Submit" onclick="submitted()"/>
    </form>
    </body>
    </html>
    Thanks
    Last edited by Toxinhead; Dec 9, 2007 at 04:07.

  2. #2
    SitePoint Enthusiast SoMBrA's Avatar
    Join Date
    Oct 2007
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello Toxin!
    Your problem is that you are not returning false to the onclick event, and anyway you calling the same function from two events so you have to be sure that it will halt so it never triggers twice.

    So just add to the onclick="submitted()" the return that you actually using on the onsubmit (onsubmit="return submitted()")


    But anyway i would just keep one of the two events, i think you dont need both!


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
  •