2 questions regarding Dynamically created checkboxes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ondernemer

    2 questions regarding Dynamically created checkboxes

    Hi guys,

    On my page I dynamically create different checkboxes.

    <input type="checkbox" name="ch1" value="some value"> option 1
    <input type="checkbox" name="ch2" value="some value"> option 2
    <input type="checkbox" name="ch3" value="some value"> option 3
    <input type="checkbox" name="ch4" value="some value"> option 4

    The reason they have different names is because of a different script which
    allows me to shift-select a range of checkboxes. (there are 200+ on a page).

    My Questions:

    1. How can I have an alertbox popup when NONE of the checkboxes are checked
    and the user submits the form?
    2. How could I have a "select all" option on these checkboxes?

    Any help would be so much appreciated!

    Ond.


  • Ulrik Skovenborg

    #2
    Re: 2 questions regarding Dynamically created checkboxes

    Ondernemer wrote:[color=blue]
    > 1. How can I have an alertbox popup when NONE of the checkboxes are checked
    > and the user submits the form?[/color]

    Put this in the javascript-section:
    function validate(frm) {
    var valid = false;
    for (i=0;i<frm.leng th;i++) {
    if (frm.elements[i].type == "checkbox" && frm.elements[i].checked) {
    valid = true;
    break;
    }
    }
    if (!valid) {alert("Please select at least one checkbox");}
    return valid;
    }

    When you submit the form you can call this function and it will
    determine whether at least one checkbox is checked:
    <form action="page.ph p" onsubmit="retur n validate(this); ">
    [color=blue]
    > 2. How could I have a "select all" option on these checkboxes?[/color]
    Try this function:
    function selectAll(frm,c heckedStatus) {
    for (i=0;i<frm.leng th;i++) {
    if (frm.elements[i].type == "checkbox") {
    frm.elements[i].checked = checkedStatus;
    }
    }
    }

    It runs through the form, finds the checkboxes and sets the
    checkedstatus to whatever you want. You can call them from the form like
    this:

    <form action="page.ph p" onsubmit="retur n validate(this); ">
    <input type="button" value="Select All"
    onclick="select All(this.form,t rue);">
    <input type="button" value="DeSelect All"
    onclick="select All(this.form,f alse);">

    Comment

    • Ondernemer

      #3
      Re: 2 questions regarding Dynamically created checkboxes

      You simply......... rock!


      Comment

      • Ulrik Skovenborg

        #4
        Re: 2 questions regarding Dynamically created checkboxes

        Ondernemer wrote:[color=blue]
        > You simply......... rock!
        >[/color]

        Well, thank you very much.

        However, if you have the exact number of checkboxes and they have the
        same type of name (i.e. "ch1","ch2" and so on) you could use this to
        avoid running through the whole form:
        <script type="text/javascript">
        function validate(frm,nu m) {
        var valid = false;
        for (i=1;i<num;i++) {
        if (frm.elements["ch"+i].checked) {
        valid = true;
        break;
        }
        }
        if (!valid) {alert("Please select at least one checkbox");}
        return valid;
        }

        function selectAll(frm,n um,checkedStatu s) {
        for (i=1;i<=num;i++ ) {
        frm.elements["ch"+i].checked = checkedStatus;
        }
        }
        </script>

        and start them just as before but with one more argument:
        <form onsubmit="retur n validate(this,5 );">
        <input type="button" value="Select All"
        onclick="select All(this.form,5 ,true);">
        <input type="button" value="DeSelect All"
        onclick="select All(this.form,5 ,false);">

        5 is the number of checkboxes - you probably should change that ;-)

        Comment

        Working...