views:

39

answers:

5

i have 6 element and i want to select the 5th element (with jQuery)

$(".deskripsi")[2]

then give it a method, but when i try it it give me error in console log

$(".deskripsi")[2].innerHTML is not a function

can somebody tell my how to select element array in jQuery and give it method.

oke, may it's seems my question is still unclear. my point is to select that element (array number 5), innerHTML is just only for to see in my console log if i select the right target.

please let me give another example. let say i have 6 list like this

<ul>
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
<li>E</li>
<li>F</li>
</ul>

now i want to select the li number 5 (i use innerHTML only for check the li value)

Thanks

+2  A: 

Try using :eq(n) and html():

$(".deskripsi:eq(2)").html();
Dominic Rodger
the answer is pretty simple :), but is really right.. thank you IT's work now :D
GusDe CooL
A: 

Did you try:

$(".deskripsi").eq(2)

The eq() method returns a wrapped jQuery set with one element, hence you can still invoke jQuery operations. This is not the case with [2] where you get the raw DOM element.

On the jQuery wrapped "set" (of one element) you can then use the .html() method.

jensgram
A: 

Try the jQuery eq function:

$('.deskripsi').eq(2).html(); // Retruns the HTML of the element
Fred Bergman
A: 

If your browser supports CSS3, you may do:

$(".deskripsi:nth-child(5)").something();

Otherwise you must ensure that jQuery selector returns multiple entries - assign results to variable and access one of them. As my predecessors said - you can also use "eq()" function. ;]

Tomasz Kowalczyk
jQuery uses its own selector engine and makes smart decisions about when to use the browser's. *:nth-child()* will work in any browser that jQuery supports.
Andy E
The point of `:nth-child()` (http://api.jquery.com/nth-child-selector/) in jQuery is to be independent of CSS3 support, I guess.
jensgram
this night, i will test the code you give me. hope this will work. thx
GusDe CooL
J-P
if you like my code, please upvote ;]
Tomasz Kowalczyk
@J-P: funny you should mention that, I intended to remove the word "smart" and leave decisions to make the comment a little less subjective, but I probably got distracted. By "smart" I meant that when parsing a selector, jQuery/Sizzle *might* decide to use the native implementation of *querySelectorAll()*, if available.
Andy E
@Tomasz: The reason I didn't up vote your answer wasn't because I didn't like it, it was because we don't have any idea of what the HTML markup looks like. *:nth-child()*, will select elements that are the nth child of their parent. If each *.deskripsi* element has a different parent element your selector will not work. *:eq()* is more suitable in this situation.
Andy E
i'm sorry tomasz :nth-child(5) not working :(
GusDe CooL
A: 

Hello,

I suspect you are trying to write

 $(".deskripsi")[2].innerHTML()

instead, use

 $(".deskripsi")[2].innerHTML

or

 $(".deskripsi").eq(2).html()

I hope this will help you

Jerome Wagner

Jerome WAGNER