0

I have something that depending if it is clicked or unclicked will do something. The thing is, that i tried to do an onclick and it will not fire. Is there some other thing that is needed for selecting/unselecting a checkbox?

ASP:

<div id = "gridDiv">
    Turn on/off some code: 
    <asp:Checkbox runat="server" name = "gridlock" id = "gridLockAttribute" />
</div>

ClientSide:

$("#gridLockAttribute").click(function(){
   try{
      alert("test");
   }catch(err){

   }
});

It doesnt seem to alert.

Fallenreaper
  • 10,222
  • 12
  • 66
  • 129
  • That should be fine as long as you run that javascript inside a $(document).ready( – Isaac Fife Aug 14 '12 at 21:54
  • Double check that the element exists before registering the click handler (e.g. by wrapping the method in a [dom ready function](http://api.jquery.com/ready/)). You can also try using [`.change()`](http://api.jquery.com/change), though the click should be triggered too. – nbrooks Aug 14 '12 at 21:54
  • you should `console.log(err)` or `alert(err)` in the `catch` block... – ahren Aug 14 '12 at 21:56
  • I current have it wrapped in a $(window).load(function(){... WHich pretty much does the same thing. – Fallenreaper Aug 14 '12 at 21:56
  • it doesnt hit the breakpoints to get to the code, it wasnt registering a "click" in my definition – Fallenreaper Aug 14 '12 at 21:57

1 Answers1

3

ASP.NET may be name-mangling your ID if the control is within another control, so things like $("#gridLockAttribute") won't work. You have to use either:

$("#<%= gridLockAttribute.ClientID %>")

Or:

$('[id$=gridLockAttribute]')

I'd prefer the first method.

Furthermore, if you are trying to get the checkbox to cause a postback automatically, you'll need to set the AutoPostBack attribute on the checkbox to True.

FishBasketGordo
  • 22,904
  • 4
  • 58
  • 91