tags:

views:

62

answers:

1

Hi, let's say I have some links

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

How do I get the value of target attribute starting with "UniqueString_" ? The ID of element is unknown, we need to search by matched "UniqueString" in "target"

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

<a id="myID" href="someurl&color=black"></a>
+6  A: 

Your first request seems like you want to seek out any link whose target attribute begins with a specific value. If that is the case, the following selector will work. You stated that you needed the full target attribute, so I'm pulling that out via the $.attr() method.

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

Now as for appending that to the HREF value of another link:

var link = $("a#myID");
$(link).attr("href", $(link).attr("href") + "&color=" + fullTarget);
Jonathan Sampson
+1! He could set the new URL with something like this: `$("#myId").attr('href', '/someurl?color=' + fullTarget.replace('UniqueString_','') )`
Doug Neiner
True, Doug. I'm a little confused by the second request, but I think you're right :)
Jonathan Sampson
Thanks a lot guys
Alex