tags:

views:

42

answers:

3

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/"&gt;yeah, link</a>
<a href="http://google.com/"&gt;g, link</a>
+4  A: 

Use the substring/contains selector:

$("selector[name*='mystring']")

finds attribute name="somemystring"

If you're looking for the whole word, delimited by spaces, then use the whole word selector:

$("selector[name~='mystring']")

finds attribute name="some mystring is good"

EDIT I took your code from above and created a jsFiddle for it. I see the one 'Hello' alert pop-up. (I didn't do an alert for the alert(ref), as I didn't know what ref was.) So the selector works for your href attributes. You're alising jQuery different than normal (normal = $, you're using $j). Did you register this alias properly? If you use $ or jQuery instead, does your code work then?

David Hoerster
almost, i'll update my question to show you what I have so far. I can't get it to work.
DerNalia
+2  A: 

You can use the attribute contains selector (*=), like this:

$("a[href*=somedomain.com]")

There are other attribute selectors available as well.

Nick Craver
+1  A: 

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'))
Adam