Checkbox

Discussion in 'JavaScript' started by star, Mar 31, 2005.

  1. #1
    Hi Experts,
    I need some coding in javascript using checkbox. I got some 5 checkboxes options. There r 2 buttons users can choose more than 1 check box and clicks the button 1 and it is stored into the dbase.

    Only 1 checkbox should be allowed to choose if the user chooses the button 2. If the user chooses more than 1 option an alert message should be displayed. Pls help. Anybody got any sample coding for this. Suggestions r highly appreciated. Thank You.

    Regards,
    Star
     
    star, Mar 31, 2005 IP
  2. marty

    marty Peon

    Messages:
    154
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The are a ton of ways to do what you need. Unfortunately, without knowing what browsers and browser versions you are supporting it's impossible to give you a definitive solution, but this should get you started for on IE5+, NN6+, Mozilla, and Safari:

    <input type="checkbox" name="ddd" value="" checked>
    <input type="checkbox" name="ddd" value="" checked>
    <input type="checkbox" name="ddd" value="">
    <input type="checkbox" name="ddd" value="">
    <input type="submit" name="sdfsd">
    <script language="JavaScript" type="text/javascript">

    //alert(document.getElementsByTagName("input").length)

    var foundCount = 0
    for(i=0;i<document.getElementsByTagName("input").length;i++)
    {
    if(document.getElementsByTagName("input").type == "checkbox")
    {
    //alert(document.getElementsByTagName("input").checked)
    if(document.getElementsByTagName("input").checked == true)
    {
    foundCount++
    }
    }

    }

    if(foundCount > 1){
    alert("Emergency Will Robinson")
    }

    </script>

    Good luck :)
     
    marty, Mar 31, 2005 IP