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 7 of 7
  1. #1
    New Coder
    Join Date
    Mar 2004
    Location
    Sydney Australia
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Has checkbox been checked or unchecked?

    I am trying to determine if the user has checked or unchecked a checkbox, the following code always seems to be running the if condition:
    ------------------------------------------
    var mssge
    var checked = false;
    function testing(catId)
    {
    for (var i=0; i < editEvent.eventCategories.length; i++)
    {
    if(editEvent.eventCategories[i].checked == true)
    {
    checked = true;
    mssge = ("you have checked : " + catId);
    break;
    }
    else if(!checked)
    {
    mssge = ("you have unChecked : " + catId);
    }

    }
    alert (mssge);
    }


    <input type="checkbox" name="eventCategories" onClick="testing(<%= intVal %>);">
    -------------------------------------------

    Please note : There are multiple checkboxes with the same name. The way the page is set up I would like to post a commer delimited string to the destination page (All the checkboxes have dynamic values)

    I have been at this all morning. I am starting to believe my objective isnt all that strait forward, of course of they had unique names it would be.

    I am looing forward to finding out if this is possible

    TYIA your help is appreciated
    wind is your friend

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    When your referencing the checkbox you need document.editEvent.eventCategories.length. Change them lines and see what you get...

  • #3
    New Coder
    Join Date
    Mar 2004
    Location
    Sydney Australia
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Brandoe85

    Thank you for your reply. I believe the 'document' part of the DOM is optional, this makes no difference to the function. Another version of it that fails to work:

    function testing(catId)
    {
    var mssge = 'unChecked';
    for (var i=0; i < document.editEvent.eventCategories.length; i++)
    {
    if(document.editEvent.eventCategories[i].checked)
    {
    mssge = 'checked';
    break;
    }
    }
    alert('You have ' + mssge + ': ' + catId);
    }
    wind is your friend

  • #4
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Odd...it fills my javascript console with errors without the document...

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,966
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Quote Originally Posted by mat41
    I believe the 'document' part of the DOM is optional, this makes no difference to the function.
    No. IE just makes all named and id'ed elements as global references so directly accessing a form by its name work fine.

    But if you use Firefox and other browsers, it will throw an error if 'document' is not specified. That behavior is correct because that's the proper way of accessing elements.

  • #6
    New Coder
    Join Date
    Mar 2004
    Location
    Sydney Australia
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, very odd. I am obviously not a JS expert, I have been lead to believe it is optional would be a more accurate thing to say. This is where a moderator comes in handy.

    Anyhow the solution:

    <input type="checkbox" onClick="testing(this,<%= getSecond(0) %>);"...

    function testing(oneelement, catId)
    {
    var status = 'unChecked';

    if (oneelement.checked)
    {
    status = 'checked';
    alert('You have ' + status + ': ' + catId);
    }
    else
    {
    alert('You have ' + status + ': ' + catId);
    }

    Yes, I'm still freaking out about thee not being a for loop in there. The problem got solved at:
    http://www.tek-tips.com/viewthread.c...=964029&page=1

    Have a fine day
    Last edited by mat41; 12-08-2004 at 04:22 AM.
    wind is your friend

  • #7
    New Coder
    Join Date
    Mar 2004
    Location
    Sydney Australia
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    glenngv

    Ahhhh it all becomes clear. Thank you, a useful piece of information.

    All
    I hope it is not inappropriate placing the link to tek-tips where the solution came from. Also, I am not usually a person who posts the same question to multiple forums, this one I thought was not do-able (since the checkboxes all had the same name) I felt this way I would get a solution quicker

    TYVM
    wind is your friend


  •  

    Posting Permissions

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