1
$(".associated_requests")
[<div class=​"associated_requests" id=​"0">​</div>​, 
<div class=​"associated_requests" id=​"1">​</div>​, 
<div class=​"associated_requests" id=​"2">​</div>​, 
<div class=​"associated_requests" id=​"3">​</div>​]

$(".associated_requests#0")
[]

Am I doing something syntactically wrong? Looking here it seems like this should be possible.

EDIT: The following also fail:

$(".associated_requests #0")
[]
$("#0 .associated_requests")
[]
$("#0.associated_requests")
[]
Community
  • 1
  • 1
maxko87
  • 2,892
  • 4
  • 28
  • 43

4 Answers4

7

Prior to HTML5, ids cannot start with numbers. Change it to start with a letter. i.e. item0, item1, item2 etc.

Jeff
  • 13,943
  • 11
  • 55
  • 103
  • 1
    @j08691 however without an html5 doctype it will not work. This is most likely the case. – Kevin B Jul 26 '12 at 17:45
  • @KevinB - then it wouldn't really be HTML5 would it? – j08691 Jul 26 '12 at 17:45
  • @j08691 What does that have to do with anything? The code above is failing because he is not using HTML5 and his id starts with a number. If he was using HTML5 he would get a single element. – Kevin B Jul 26 '12 at 17:47
  • @KevinB - why technically not legal in < HTML5, most browsers will still interpret it correctly. – j08691 Jul 26 '12 at 17:49
  • Too many browsers will puke and too many scripts will look weird to have digits as ID or name. I would avoid it like the plague – mplungjan Jul 26 '12 at 17:52
  • I am using HTML5 for my rails app. Still, the number id's weren't working for some reason. I changed it to start with letters and it now works, thanks! – maxko87 Jul 26 '12 at 17:55
2

Why not just do $('#item0')? .. You should not have duplicate id's

The Alpha
  • 143,660
  • 29
  • 287
  • 307
labroo
  • 2,901
  • 3
  • 27
  • 36
0
var item0 = document.getElementById('0');

var item1 = document.getElementById('1');

or you can just rename your id instead of just a number add like id="id-0"

so you can select it as $('#id-0.associated_content')

Jeff
  • 13,943
  • 11
  • 55
  • 103
Johndave Decano
  • 2,101
  • 2
  • 16
  • 16
0

if you want to select the divs based on their indices, as an alternative you can use eq() method:

$('.associated_requests').eq(0) // selects the first one
$('.associated_requests').eq(1) // selects the second one
Ram
  • 143,282
  • 16
  • 168
  • 197