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>
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".
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.
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).
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.
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?
You can use the attribute contains selector (*=
), like this:
$("a[href*=somedomain.com]")
There are other attribute selectors available as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With