Get the value of the target attribute if it matches some string through jQuery

let's say i have links

<a href="#" target="UniqueString_black"></a>
<a href="#" target="dog"></a>
<a href="#" target="duck"></a>

How to get the value of the target attribute starting with "UniqueString_"? The item ID is unknown, we need to search for "UniqueString" in "target"

I also need to bind this value as a link parameter to a known element

<a id="myID" href="someurl&color=black"></a>
+3
source share
1 answer

Your first request seems like you want to find any link whose target attribute starts with a specific value. If so, the next selector will work. You stated that you need the full target attribute, so I pull it out using the method $.attr().

var fullTarget = $("a[target^='UniqueString_']").attr("target");

, HREF :

var link = $("a#myID");
$(link).attr("href", $(link).attr("href") + "&color=" + fullTarget);
+7

Source: https://habr.com/ru/post/1728691/


All Articles