Alex Rivera | Logout

jQuery: how do I check if an element attribute contains part of a string?

Asked 2010-08-31T16:53:13.813
10

Like, I want to check if a link has a certain domain, or already has parameters attached to the end.

is there a way of doing a regex thing like ~= maybe?

currently, I have

    alert(ref);
        $j("a[href*='mysite']").each(function(i){
            alert("hello");
            $j(this).attr('href',$j(this).attr('href') + "?ref=" + $j.cookie.get("tb_ref"));
        }); 

but the selector isn't working. (I never see the Hello alert, but I seed the alert ref alert.

my a tags

<a href="http://domain.mysite.com/">yeah, link</a>
<a href="http://google.com/">g, link</a>
Edit
Report

1 Answer

1

If the link is <a id='link' href='www.google.com/myparam'></a> and you want to tell if the link contains myparam use jQuery/javascript:

/myparam/.test($("#link").attr('href'))
answered 2010-08-31T17:00:24.420

Your Answer