Put var inside jquery: gt ()

I have a list with display:none and some code to show element 3:

HTML:

 <ul> <li>item 1</li> <li>item 2</li> <li>item 3</li> <li>item 4</li> </ul> 

jquery:

 var item = '1'; $("li:gt(1):lt(1)").show(); 

Is it possible to get var inside gt select? Al, I can think it

 $("li:gt('+item+'):lt(1)").show(); 

Does not work. It can be done? If not what else can I try?

Example: http://jsfiddle.net/BSakQ/5/

+6
source share
2 answers

You need to delimit the string with the correct quotation marks. Try the following:

 $("li:gt(" + item + "):lt(1)").show(); 
+11
source

You must use double inverted comma for conacatination beacause, since you use double inverted comma for selector.

Try this jquery code:

 $("li:gt("+item+"):lt(1)").show(); 
-1
source

All Articles