www.webdeveloper.com
Results 1 to 10 of 10

Thread: Help me with this code I cannot seem to get it to work!

  1. #1
    Join Date
    Feb 2013
    Posts
    6

    Question Help me with this code I cannot seem to get it to work!

    I need to make this function work for a school project
    which requires a form to be validated. The rest of the code works but I cannot seem to get this bit to. I need to make it so when one of the radio buttons is clicked a confirmation box is brought up, with the name of the button pressed.

    Eg. "You have selected ______. Are You Sure"

    The blank space is where the bit needs to be.
    Then when cancel is pressed the radio buttons are all unchecked.

    It also must be in one function. Any help would be appreciated.

    Here is what I have so far:

    <html>
    <head>

    <script language="JavaScript">
    function checkbox() {
    var confirmboxyes = confirm("You've selected. Are you sure?");

    if ( confirmboxyes == false){
    document.getElementById("1").checked = false;
    document.getElementById("2").checked = false;
    document.getElementById("3").checked = false;
    return true
    }
    else {
    return false;

    }
    }
    </script>

    </head>
    <body bgcolor="pink">

    <form name="form">
    Cats <input type="radio" name="test" value="Cats" id="1" onclick="return checkbox()"> <br>
    Dogs <input type="radio" name="test" value="Dogs" id="2" onclick="return checkbox()"> <br>
    Birds <input type="radio" name="test" value="Birds" id="3" onclick="return checkbox()">
    </form>

    </body>
    </html>

  2. #2
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,270
    ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

  3. #3
    Join Date
    Feb 2013
    Posts
    6
    Ive made alterations to the code but I cannot seem to get the value of the radio buttons to be displayed in the confirm box, i was getting "invalid" for a while, but now it wont work entirely.

    Heres what I have changed:


    <html>
    <head>

    <script language="JavaScript">
    function checkbox() {
    var confirmboxyes = confirm("You've selected " + Selected + ". Are you sure?");
    var Selected = "";
    if (document.getElementsById("cats").checked == true) {Selected = document.getElementsById("cats").value;}
    if (document.getElementsById("dogs").checked == true) {Selected = document.getElementsById("dogs").value);}
    if (document.getElementsById("birds").checked == true) {Selected = document.getElementsById("birds").value);}


    if ( confirmboxyes == false){
    document.getElementById("cats").checked = false;
    document.getElementById("dogs").checked = false;
    document.getElementById("birds").checked = false;
    return true
    }
    else {
    return false;

    }
    }
    </script>

    </head>
    <body bgcolor="pink">

    <form name="form">
    Cats <input type="radio" name="test" value="Cats" id="cats" onclick="return checkbox()"> <br>
    Dogs <input type="radio" name="test" value="Dogs" id="dogs" onclick="return checkbox()"> <br>
    Birds <input type="radio" name="test" value="Birds" id="birds" onclick="return checkbox()">
    </form>

    </body>
    </html>

  4. #4
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by alpinefury View Post
    Code:
    function checkbox() {
     var confirmboxyes = confirm("You've selected " + Selected + ". Are you sure?");
    At this point 'Selected' is undefined, so how can you expect to display it ?

    Code:
    if (document.getElementsById("cats").checked == true)    {Selected = document.getElementsById("cats").value;}
    Incorrect spelling - use the error console.


    Code:
    if (document.getElementsById("dogs").checked == true)    {Selected = document.getElementsById("dogs").value);}
        if (document.getElementsById("birds").checked == true)    {Selected = document.getElementsById("birds").value);}
    Syntax errors as shown.




    You assign values to 'Selected' but the variable is not being used anywhere.

    To display the button's value do this:

    Code:
    Cats <input type="radio" name="test" value="Cats" id="cats" onclick="return checkbox( this )">
    
    
    /* then */
    
    function checkbox( elem ) 
    {
     var confirmboxyes = confirm("You've selected " + elem.value + ". Are you sure?");

  5. #5
    Join Date
    Feb 2013
    Posts
    6
    Slight problem again, no matter what I press on the Confirm Box ie. Confirm/Cancel all of the radio buttons are cleared how do I rectify this?

  6. #6
    Join Date
    Feb 2013
    Posts
    6
    And heres what I've got

    <html>
    <head>

    <script language="JavaScript">
    function checkbox(elem) {

    var confirmboxyes = confirm("You've selected " + elem.value + ". Are you sure?");

    if (confirmboxyes == false){
    document.getElementById("cats").checked = false;
    document.getElementById("dogs").checked = false;
    document.getElementById("birds").checked = false;
    return true
    }
    else {
    return false;

    }
    }
    </script>

    </head>
    <body bgcolor="pink">

    <form name="form">
    Cats <input type="radio" name="test" value="Cats" id="cats" onclick="return checkbox( this )"> <br>
    Dogs <input type="radio" name="test" value="Dogs" id="dogs" onclick="return checkbox( this )"> <br>
    Birds <input type="radio" name="test" value="Birds" id="birds" onclick="return checkbox( this )">
    </form>

    </body>
    </html>

  7. #7
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by alpinefury View Post
    Slight problem again, no matter what I press on the Confirm Box ie. Confirm/Cancel all of the radio buttons are cleared how do I rectify this?
    Show the entire code in its current state, this time within [ code][/code] tags (press # button).

  8. #8
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by alpinefury View Post
    Slight problem again, no matter what I press on the Confirm Box ie. Confirm/Cancel all of the radio buttons are cleared how do I rectify this?
    If you return false, the clicked button is cleared even if you don't clear it.

  9. #9
    Join Date
    Feb 2013
    Posts
    6
    so how do i make it so it works? sorry about the silly question new to JavaScript

  10. #10
    Join Date
    Feb 2013
    Posts
    6
    I've sorted it out now, thanks for the help!

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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