www.webdeveloper.com
Results 1 to 4 of 4

Thread: checkboxes - how to check them when they're dynamicaly named

Hybrid View

  1. #1
    Join Date
    May 2003
    Posts
    44

    checkboxes - how to check them when they're dynamicaly named

    hey guys

    I have an ASP page which displays a recordset - each record in the rs has a dynamically named checkbox (ie. <%="InvoiceRun_" & rs.RecordID%>. It all works fine, except I have a little JS function that used to check all my checkboxes, back when my checkboxes were statically named:

    function checkall(formname,checkname,thestate){
    var el_collection=eval("document.forms."+formname+"."+checkname)
    for (c=0;c<el_collection.length;c++)
    el_collection[c].checked=thestate
    }

    I used to call this function like so:

    <a href="javascript:checkall('approve', 'InvoiceRun',true)">Tick All</a>

    What I'd like to know is whether anyone has any clue how to get it to check all the boxes now that they're not all called "InvoiceRun". They all still START with "InvoiceRun", but end in the recordID. Some kind of wildcard would be good, only my javascript doesn't run to that....

    Cheers

    R

  2. #2
    Join Date
    Nov 2002
    Location
    Morden, MB, Canada
    Posts
    42
    Just an idea, but is there any way you can keep the checkboxes named the same, but assign them different values?

    <input type="checkbox" name="InvoiceRun" value="<%=rs.RecordID%>">

    This way, you can still reference the checkboxes using document.formname.InvoiceRun[c], as well as uniquely identifying all the checkboxes by "value=". This may or may not work for you depending on how the rest of your page uses these checkboxes, but it's hard to say more without knowing more specifics about your page.

    Hope this helps,
    TBor

  3. #3
    Join Date
    May 2003
    Posts
    44

    Wink

    nah, fraid not.

    I need the names of the checkboxes to be unique due to a bunch of ASP/SQL updates going on on the page, which require them to be unique. That took me about a week to write, so no way am I gonna change it now!

  4. #4
    Join Date
    Nov 2002
    Location
    Morden, MB, Canada
    Posts
    42
    OK, option #2 - not as elegant, but something to try:

    for each checkbox, give it a class ID, something like this:

    <input type="checkbox" name="<%="InvoiceRun_" & rs.RecordID%>" class="chkbox">

    This class ID doesn't have to be linked to anything, just a unique identifier for all checkboxes. This way, you can find & check all chekcboxes like this (VBScript, sorry!):

    for each element in document.formname.elements
    if element.classname="chkbox" then
    element.checked = true
    end if
    next

    This isn't elegant - it's actually rather ugly. It feels like more of a workaround than a solution, but it'll work. I'm sure there's a lot of better ways to do this though, so anyone else with ideas, go right ahead!

    Hope this helps,
    TBor

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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