views:

383

answers:

3

Hello, I"m trying to attached some jquery to checkboxes in a gridview, using document.ready:

$(document).ready(function() { var chkBox= document.getElementById("gvTimeSheet_ctl01_chkAll1"); //I can alert chkBox.id, element exists

       var name = $("input[name='gvTimeSheet$ctl01$chkAll1']");
       //Here, when I alert the id, I get a null

       var ID = $("#gvTimeSheet_ctl01_chkAll1");
      //Here, when I alert the id, I get a null

       var withClass = $(".chkAll1Class");
       //Here, when I alert the id, I get a null

       var withClass2 = $(".Bill1");
       //Here, when I alert the id, I get a null

       //This line causes the browswer to crash and gives me the following error
       //Microsoft JScript runtime error: 'null' is null or not an object
       $("#gvTimeSheet_ctl01_chkAll1").click(function()           
       {
           var checked_status = this.checked;
           $("input[class=Bill1]").each(function()
           {
           this.checked = checked_status;
           });

       });

    });*/

So, why are any attempts at finding an object null in jquery, yet exist in regular javascript within the same method? What am I missing here. I have the jquery js files brought in in a script tag directly above this method. I just can't seem to find any objects on this page with jquery. On other pages, I can....help.

Terry

+2  A: 

Objects that result from a jQuery selector are actually wrappers around a DOM object, so you don't access it the same as a DOM object.

If you're alerting just "name.id", from your first example above, there won't be any such property on the jQuery wrapper. Try alerting your ID as follows:

alert(name.attr("id"));
womp
+1  A: 
 var ID = $("#gvTimeSheet_ctl01_chkAll1");

This returns a jQuery object, not an ID. ID.id would also be undefined. To get the ID, you need:

var ID = $("#gvTimeSheet_ctl01_chkAll1").attr("id");
Craig Stuntz
A: 

Does the page you're adding this code to already include the Prototype JavaScript library?

jQuery's "$" method never returns null, so this shouldn't be a problem:

// This line causes the browswer to crash and gives me the following error
// Microsoft JScript runtime error: 'null' is null or not an object
$("#gvTimeSheet_ctl01_chkAll1").click(function() { .... });

All the comments about needing to use .attr('id') still stand (though I prefer $('#whatever')[0].id myself.)

searlea