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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JavaScript button with limit

    ne1 help me add a limit of 3 to this function?

    var i;
    var cRecs;
    cRecs = "";
    for (i = 0; i < document.forms[0].length; i++){
    if (document.forms[0].elements[i].type == "checkbox"){
    if (document.forms[0].elements[i].checked==true){
    cRecs = cRecs + document.forms[0].elements[i].name;
    if (i < document.forms[0].length){
    cRecs = cRecs + ",";
    }
    }
    }
    }
    if (cRecs == ""){
    alert("No records have been selected!");
    } else {
    window.open("rpt.asp?recs=" + cRecs, "Report");
    }
    }

    Thanks in advance

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,011
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Use this example.

    Code:
    <form id = "myform">
    <input name = "myname" type = "checkbox" value = "Red" onclick="count()"> RED <br>
    <input name = "myname" type = "checkbox" value = "Blue" onclick="count()"> BLUE <br>
    <input name = "myname" type = "checkbox" value = "Green" onclick = "count()"> GREEN <br>
    <input name = "myname" type = "checkbox" value = "Yellow" onclick = "count()"> YELLOW<br>
    <input name = "myname" type = "checkbox" value = "Purple" onclick = "count()"> PURPLE<br>
    <input name = "myname" type = "checkbox" value = "Grey" onclick = "count()"> GREY<br>
    
    <br>
    <br>
    You have checked <input type = "text" name= "totChecked" id = "totChecked" size = "2" value = "0" readonly> checkboxes
    </form>
    
    <script type = "text/javascript">
    function count() {
    var f = document.getElementById("myform");
    var len = f.myname.length;
    var totBoxes = 0;
    for (i = 0; i < len; i++) {
    if (f.myname[i].checked) { 
    totBoxes ++; 
    }
    if (totBoxes >3) {
    alert ("You may only check a maximum of 3 boxes");
    f.myname[i].checked = false;
    totBoxes --;
    }
    }
    f.totChecked.value = totBoxes;
    }
    
    </script>

    Quizmaster: How many days will there be in the year 2013?
    Contestant: Er... is it 60 or 52?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    For an example of the modern way to do this that doesn't have the JavaScript jumbled together with the HTML see http://www.felgall.com/jstip141.htm
    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
    •