0

I using each() but only return the contents of first element...

For example: http://jsfiddle.net/UserNaN/L8pA5/

How to get all the contents with div have same ID is #title

Please help me!

UserNaN
  • 137
  • 2
  • 13
  • possible duplicate of [How to select all elements with a particular ID in jQuery?](http://stackoverflow.com/questions/902839/how-to-select-all-elements-with-a-particular-id-in-jquery) – Karl-André Gagnon Jul 01 '14 at 15:19

3 Answers3

2

It will surely fail since ids have to be unique.

Use classes instead. You've already set active class to the div.

So, you can do:

$('.active').each()
Amit Joki
  • 58,320
  • 7
  • 77
  • 95
  • @UserNaN but you didn't accept mine? Anyway glad to have helped – Amit Joki Jul 01 '14 at 16:05
  • I'm sorry if my actions do you not happy... I want to solve the problem from [here](http://stackoverflow.com/questions/24424324/how-to-convert-html-contents-to-json-using-php), but doesn't seem to have any answers can help me. Therefore, I have to "solve" from the client, and the answer from @pumpkinzzz is more obvious. If you do not feel upset, please see [my previous question](http://stackoverflow.com/questions/24424324/how-to-convert-html-contents-to-json-using-php), thanks! – UserNaN Jul 01 '14 at 16:27
  • @UserNaN its not that I'm unhappy or something like that. I answered it first so, just asked. Cheers! – Amit Joki Jul 01 '14 at 16:29
2

Never use same ID for multiple elements. ID has to be unique of course. For your purposes you should use classes

So:

  1. Remove ID "title" from all the elements and move it to the class property: <div class='title active' data='1'>
  2. Use ".title" as selector $('#container').find('.title').each(...

http://jsfiddle.net/L8pA5/1/

pumpkinzzz
  • 2,907
  • 2
  • 18
  • 32
-1

use $("div#title").each(function(){});

I was facing same problem. Its working for me.

serenesat
  • 4,611
  • 10
  • 37
  • 53