SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Sep 2004
    Location
    USA
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    error: document.getElementById() has no property

    hello guys,

    I have a function which selects and deselects all the checkboxes in a form. I also need to change color upon selection. I need to change color upon selection of a single checkbox too. But, I am getting error "document.getElementById() has no property in Mozilla

    Here is my function

    function CheckAllBoxes(FormName, FieldName,chkvalue)
    {
    if(!document.forms[FormName])
    return;
    var objCheckBoxes = document.forms[FormName].elements[FieldName];
    if(!objCheckBoxes)
    return;
    var countCheckBoxes = objCheckBoxes.length;

    if(!countCheckBoxes){
    if(objCheckBoxes.checked) {
    objCheckBoxes.checked = chkvalue;
    }

    }else{ // set the check value for all check boxes
    for(var i = 0; i < countCheckBoxes; i++){
    objCheckBoxes[i].checked = chkvalue;
    }
    }
    if(chkvalue) {
    document.getElementById('foldertable').style.background='#d5d7de';
    document.getElementById('datatable').style.background='#d5d7de';

    for(var i = 0; i < countCheckBoxes; i++){
    document.getElementById('ft' + i).style.background='#d5d7de';
    document.getElementById('dt' + i).style.background='#d5d7de';
    }
    } else {
    document.getElementById('foldertable').style.background='#dfe3e9';
    document.getElementById('datatable').style.background='#dfe3e9';
    for(var i = 0; i < countCheckBoxes; i++){
    document.getElementById('ft' + i).style.background='#dfe3e9';
    document.getElementById('dt' + i).style.background='#dfe3e9';
    }

    }
    }

    where 'ft' and 'dt' are rowid's.

    Thank for any ideas?

  2. #2
    SitePoint Addict
    Join Date
    Sep 2004
    Location
    Denver
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    newperllearner,

    You're trying to do 2 diffirent tasks in 1 function, and that's not a very good idea. I haven't looked much into your code, but to select and deselect, all you have to do is get an array of all of your check boxes.
    Code:
    var chckBoxArray = document.getElementsByTagName('CHECKBOX');
    Next, you loop through that array and select/deselect based on your perefrence.

    I hope this helps.

    cb

  3. #3
    SitePoint Zealot
    Join Date
    Sep 2004
    Location
    USA
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks cbiti for your help..

    But this doesn't work for me. Checking and unchecking is not a problem. It works fine and functions as it should be. Problem area is coloring. I need to color the selected checkboxes and when they are de-selected their color should change back to original color.

    Though I am getting the error, the function works fine in IE, Mozilla and Netscape. But with this error, my jsp doesn't look professional, so I need to get rid of this error!

    Can anybody help me out?

    Thanks

  4. #4
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The solution posted by cbiti won't work, because there are no <CHECKBOX> elements in HTML. You could use document.getElementsByTagName("input") and apply the code only to those elements whose "type" attribute has the value "checkbox", though.

    The problem seems to be an invalid element ID somewhere. Make sure that you really have elements with id="foldertable" and id="datatable", and all the id="ft0", id="dt0", etc.
    Birnam wood is come to Dunsinane

  5. #5
    SitePoint Zealot
    Join Date
    Sep 2004
    Location
    USA
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks AutisticCuckoo,

    earlier my function wasn't able to find a rowid which I spelled incorrectly. Now the function works just fine as I wanted.


Bookmarks

Posting Permissions

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