Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: If this HREF contains

Why can't I get this to work??

$("a").each(function() {
    if ($(this[href$="?"]).length()) {
        alert("Contains questionmark");
    }
});

Ps.: This is just at simplifyed example, to make it easier for you to get an overview.

like image 596
curly_brackets Avatar asked Jun 16 '11 15:06

curly_brackets


3 Answers

You could just outright select the elements of interest.

$('a[href*="?"]').each(function() {
    alert('Contains question mark');
});

http://jsfiddle.net/mattball/TzUN3/

Note that you were using the attribute-ends-with selector, the above code uses the attribute-contains selector, which is what it sounds like you're actually aiming for.

like image 194
Matt Ball Avatar answered Nov 17 '22 10:11

Matt Ball


$("a").each(function() {
    if (this.href.indexOf('?') != -1) {
        alert("Contains questionmark");
    }
});
like image 21
Christopher Armstrong Avatar answered Nov 17 '22 10:11

Christopher Armstrong


use this

$("a").each(function () {
    var href=$(this).prop('href');
    if (href.indexOf('?') > -1) {
        alert("Contains questionmark");
    }
});
like image 4
Amir Ismail Avatar answered Nov 17 '22 11:11

Amir Ismail