SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2001
    London, UK
    0 Post(s)
    0 Thread(s)

    selecting checkboxes using JS and php

    I have a search engine with checkboxes next to each search result. I also have a link which when clicked, all the checkboxes of the results are 'ticked' or 'unticked'. The problem is that each checkbox MUST have a different name (its used for record deleting etc). Also, the number of results can vary from 10 to 500.

    Now, all i have been able to do is use some php to generate the code below:
    <SCRIPT LANGUAGE="JavaScript"><!--
    function checkboxes() {
    for($i=0; $i < $numofrows; $i++){
        $cbname = "checkbox" . $i;
        echo "document.bsearch.$;\n";
    As you can probably tell, i dont know much about javascript, and i have a feeling this would probably be much better done using an array or something. Also, im thinking that if there are 500 results, 500 lines of code just for this tiny javascript would be created, which is not good at all.

    Can anyone throw me some js code to help me out?
    Last edited by Shaq; Aug 22, 2002 at 02:50.

  2. #2
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Dallas, TX
    0 Post(s)
    0 Thread(s)
    give all the checkboxes the same name, but unique IDs

    <input type="checkbox" name="cbSet1" id="cbSet1B1" />
    <input type="checkbox" name="cbSet1" id="cbSet1B2" />
    <input type="checkbox" name="cbSet1" id="cbSet1B3" />
    <input type="checkbox" name="cbSet1" id="cbSet1B4" />

    Or whatever. Then...
    var cb = document.forms[0].elements['cbSet1']; // Or however you indentify it...
    for (var i in cb) cb[i].click();
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts