Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

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>
like image 212
NullVoxPopuli Avatar asked Aug 31 '10 16:08

NullVoxPopuli


People also ask

When working with attribute selectors how can you select elements which contain a particular attribute value?

The [attribute|="value"] selector is used to select elements with the specified attribute, whose value can be exactly the specified value, or the specified value followed by a hyphen (-). Note: The value has to be a whole word, either alone, like class="top", or followed by a hyphen( - ), like class="top-text".

How do you use getAttribute?

JavaScript getAttribute() example How it works: First, select the link element with the id js using the querySelector() method. Second, get the target attribute of the link by calling the getAttribute() of the selected link element. Third, show the value of the target on the Console window.

How Get contains in jQuery?

jQuery :contains() SelectorThe :contains() selector selects elements containing the specified string. The string can be contained directly in the element as text, or in a child element. This is mostly used together with another selector to select the elements containing the text in a group (like in the example above).

Can we use multiple selectors in jQuery?

You can specify any number of selectors to combine into a single result. This multiple expression combinator is an efficient way to select disparate elements. The order of the DOM elements in the returned jQuery object may not be identical, as they will be in document order.


2 Answers

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?

like image 158
David Hoerster Avatar answered Oct 07 '22 17:10

David Hoerster


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

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

There are other attribute selectors available as well.

like image 45
Nick Craver Avatar answered Oct 07 '22 16:10

Nick Craver