Victor
Victor

Reputation: 953

jquery attribute indexOf

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?

Upvotes: 0

Views: 2975

Answers (2)

Nick Craver
Nick Craver

Reputation: 630439

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

Upvotes: 2

derrickp
derrickp

Reputation: 203

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

 }

Upvotes: 2

Related Questions