user1391152
user1391152

Reputation: 1289

passing a text string and a variable into into href selector

I need to pass a string AND a variable as a selector into a href attribute, but I can't seem to get the syntax correct. Can anybody help?

I currently have a variable 'gallery' which I need to pass in the something like:

$('#work_gallery li a[href="http://www.ddbremedy.co.uk/siteupdate/" + gallery + ]');

Upvotes: 0

Views: 88

Answers (2)

Kevin Bowersox
Kevin Bowersox

Reputation: 94499

Switch the selector to use double quotes so you can use single quotes within the attribute selector.

$("#work_gallery li a[href='http://www.ddbremedy.co.uk/siteupdate/" + gallery + "']");

Unless you have multiple links to the same page the first part of the selector, #work_gallery li may be unnecessary. If this link only appears once on the page use:

 $("a[href='http://www.ddbremedy.co.uk/siteupdate/" + gallery + "']");

Working Example: http://jsfiddle.net/jFYGM/

Upvotes: 1

dsgriffin
dsgriffin

Reputation: 68616

Like so -

$('#work_gallery li a[href="http://www.ddbremedy.co.uk/siteupdate/' + gallery + '"]');

Upvotes: 1

Related Questions