views:

77

answers:

3

when i click on a link i take it's href value in a variable (the number represents an unique id), then...i have a separate ul:

<ul class="class_one">
   <li class="class_two"><a href="2345">some text</a></li>
   <li class="class_three"><a href="6789">some text</a></li>
</ul>

what i'm trying to do is to see if i have any "a" in my "ul" that has the same id that i stocked in my variable.

i tried something like this( of course with no result ):

$('ul.class_one').find('a[href="' + myVar + '"]')
//and if it finds no element do something
//i think is something wrong with the quotes ('a[href="' + myVar + '"]')
A: 

CSS, and jQuery by extension, uses single quotes.

$("ul.class_one").find("a[href='" + myVar + "']");
Anthony Mills
As with all JavaScript and CSS, you can interchangeably use single or double quotes as long as your boundary delimiters are of the same type.
Russ Bradberry
+1  A: 

Have you tried swapping your quotes? jQuery might be using single quotes for attribute selection matching...

$('ul.class_one').find("a[href='" + myVar + "']");

From jQuery Documentation

An attribute value. Quotes are optional in most cases, but should be used to avoid conflicts when the value contains characters like "]". Variables can be used using the following syntax: [name='" +MyVar+ "']

Quintin Robinson
i tried both versions, but doesn't get any good :(
kmunky
sorry...my bad,i mis-typed something. it works! thanks guys ;)
kmunky
Glad that you got it working!
Quintin Robinson
+1  A: 

It's possible to remove the quotes altogether...

$('ul.class_one').find('a[href='+myVar+']');

Should work

gnarf