user367864
user367864

Reputation: 281

simple jquery length return bad value

it making me crazy, i'm probably stupid :)

Go to this link : http://www.willbegood.com/lab/whatup/list2.php?categ=4

Why to this page return "1", it should be return "4". There are 4 div whith the id equal to "liste-item", why it return only 1? My page is generated by a simple php query, is this the reason?

I have also tryed to test this with the same page but static (html) : same problem Check here : http://www.willbegood.com/lab/whatup/list3.html

Thanks

Upvotes: 0

Views: 85

Answers (4)

Sushanth --
Sushanth --

Reputation: 55740

ID's in a HTML page should be unique.

If you select it by ID , it will return the first occurrence of the ID. It will not look further.

In such cases better to replace it with a class.

Replace id="liste-item" with class="liste-item"

Upvotes: 1

scrappedcola
scrappedcola

Reputation: 10572

Your issue is that you are using id's where you need to be using class. Id's by definition need to be unique. When you select by Id in jquery (or straight JS) you will only get a single element. Change your div's to

<div class='liste-item off'></div>
<div class='liste-item off'></div>
<div class='liste-item off'></div>
<div class='liste-item off'></div>

And you JS to alert($(".liste-item").length) A tutorial on id and class selectors can be found here: http://www.htmldog.com/guides/cssintermediate/classid/

Upvotes: 1

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79830

You cannot duplicate ID. ID should be unique in a DOM and the jQuery selector would return only 1 which is the first element with liste-item.

Use class instead of ID.

Upvotes: 1

wanovak
wanovak

Reputation: 6127

You are only allowed one id element. That's probably why. Change it to class.

Upvotes: 1

Related Questions