Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery if attribute contains a certain value

Tags:

jquery

Having a total mind blank here. Hoping you can help.

How would I alter this argument to be when the attribute 'href does not start with #overlay'...

if(this.getTrigger().attr("href")){
// stuff in here
}

Thanks you wonderful people. Kevin

like image 522
Kevin Avatar asked Apr 19 '26 03:04

Kevin


2 Answers

You can use slice, substring or substr:

if (this.getTrigger().attr("href").slice(0, 8) != "#overlay") {
}

or indexOf:

if (this.getTrigger().attr("href").indexOf("#overlay") != 0) {
}

or the regex test method:

if (!/^#overlay/.test(this.getTrigger().attr("href"))) {
}
like image 164
Andy E Avatar answered May 05 '26 07:05

Andy E


If you want to use jQuery selectors:

if(this.getTrigger().is('a:not([href^="#overlay]")')) {
  // stuff in here
} 

Edit: In your case where you already have only one item and want to check its href value, the selector solution performs worse than just comparing a slice of the attribute to '#overlay' as the other answers here have shown. I just posted my solution to show there is more than one way to do it.

like image 41
chiborg Avatar answered May 05 '26 07:05

chiborg