Trip
Trip

Reputation: 27114

How can one put a dynamic attribute inside this jQuery selector?

I have this object..

$object = $(".service-box:first").attr("rel")
# => "spaghetti"

I am trying to find the this object inside of a selector..

$(".hqs-text[rel=$object]")

But this is an innapropriate way to use dynamic variables in this selector.

How can I do this correctly?

Thanks!!!

Upvotes: 0

Views: 76

Answers (2)

Fisher
Fisher

Reputation: 1794

$(".hqs-text[rel="+$object+"]")

Upvotes: 1

Joseph Silber
Joseph Silber

Reputation: 219938

Simple concatenation will do:

$(".hqs-text[rel=" + $object + "]");

Or even all in one line:

$(".hqs-text[rel=" + $(".service-box:first").attr("rel") + "]");

Upvotes: 5

Related Questions