tags:

views:

100

answers:

2

When I am getting at an attribute onclick of custom(Reporting Services) checkbox it gives me correct result. However when I am trying to use indexOf on that result it says "Object doesn't support this property or method", i.e. this is fine, gives me a long string

$('input[id*=CustomCheckBox]').click(function()
{
      alert( $(this).attr("onclick") );


});

But this gives an error(object doesn't support this property or method):

$('input[id*=CustomCheckBox]').click(function()
{
     if ($(this).attr("onclick").indexOf("SomeString") > -1 )
     {
          //do some processing here

     }
}

What would I need to modify so that indexOf is working properly?

+2  A: 

I agree with Nick Carver above, but if you still need to do it, you simply need to cast the attribute as a string before you try to use indexOf. I tested it quickly in Safari and it seemed to work as expected.

if (String($(this).attr("onclick")).indexOf("SomeString") > -1 )
 {
      //do some processing here

 }
derrickp
This won't work as stated above, for example in firefox the attribute will be `undefined` :)
Nick Craver
@Nick - Seems to work for me in FF.
patrick dw
Yeah, works in FF for me as well.
derrickp
@patrick - Woops, I was testing against the wrong div ID, in any case this still isn't guaranteed to be present, the event is though.
Nick Craver
Also seems to be working in IE. Actually this solves my issue
Victor
@patrick - This is what I mean: http://jsfiddle.net/EzsCE/3/ See how the attribute isn't accurate anymore? The event gets the correct *current* function here, the attribute doesn't. Now in this case jQuery is normalizing it a bit for you (`.attr()` vs DOM `.getAttribute()`), but don't trust that this is always the case, you should, as a practice, never use `.attr("eventName")` to *reliably* get what's in use.
Nick Craver
@Nick This was the only way for me determine if it's a standard or custom checkbox, there were no other differences I could find. I could do it the way you showed but they could've used event other then `onclick`
Victor
@Victor - The other events are available this way as well, `this.onchange`, etc, everything you can add as an attribute you can get via the DOM, for example this does `this.onchange` at the same time: http://jsfiddle.net/EzsCE/4/
Nick Craver
@Nick - I see that. I had just noticed that it seemed to work in FF, and wondered what I was doing wrong to make it work. ;o)
patrick dw
+2  A: 

You can use the onclick event itself, not the most efficient, but if it's your only option... You'd do it like this:

$('input[id*=CustomCheckBox]').click(function() {
  if (this.onclick && this.onclick.toString().indexOf("SomeString") > -1 ) {
    alert('found!')
  } else {
    alert('not found :(');
  }
});

You can try a demo here

Nick Craver