www.webdeveloper.com
Results 1 to 3 of 3

Thread: JavaScript: Checkboxes. Help!

  1. #1
    Join Date
    Oct 2012
    Posts
    1

    JavaScript: Checkboxes. Help!

    hi guys,

    help me with this one..

    I have a php code to loop a checkbox; 3 checkboxes.

    <?php
    for($id=1;$id<=3;$id++){
    echo "<input type='checkbox' value='$id' id='check_$id' name='checkboxes'/>";
    }
    ?>

    Result:
    <input type='checkbox' value='1' id='check_1' name='checkboxes'/>
    <input type='checkbox' value='2' id='check_2' name='checkboxes'/>
    <input type='checkbox' value='3' id='check_3' name='checkboxes'/>

    now, i want to create a javascript or jquery codes to get whether the checkboxes are checked using onlick function.

    maybe something like this.

    <a href="#" onclick="actionDelete()" />

    function actionDelete(){
    if(document.getElementsByName("checkboxes").checked == true){
    document.mainform.action = "include/db/delete/deleteSelected.php";
    document.mainform.submit();
    }else{
    alert("Please check some checkboxes for deleting");
    }
    }

    my form name is mainform.. if i use getElementById(), then i shud also create a for loop. but i dont want to create a for loop.

    i know its easy.. so help me... Beginner!

  2. #2
    Join Date
    Aug 2008
    Location
    Sweden
    Posts
    227
    With jQuery you could do something like this to see if those three checkboxes are checked:
    Code:
    if ($("[name=checkboxes]:checked", document.mainform).length == 3)
    Without jQuery you'd have to make a loop. ;)

  3. #3
    Join Date
    Jul 2007
    Posts
    386
    jQuery makes it a lot easier. If you wanted to avoid "direct" loops you'd have to assign a class called, for example, "boxchecked" and do a getElementsByClassName().

    Go with refreezed example, it should work perfectly.

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