shaner
shaner

Reputation: 11

Find first id in jquery

<div class="activity" id="4">
<div class="activity" id="3">
<div class="activity" id="2">
<div class="activity" id="1">

and jquery code to find first id

var ID = $('.activity:first').attr('id');

whats wrong there? error in firebug

Warnung: Unbekannte Pseudoklasse oder Pseudoelement 'first'. Zeile: 0

thank you

Upvotes: 1

Views: 8260

Answers (3)

elwyn
elwyn

Reputation: 10521

Nothing seems to be wrong.

I can use your exact markup/jquery and it works just fine:

http://jsfiddle.net/6AH6T/

You might like to try tidying up your HTML and seeing if that helps - e.g. closing your DIVs, and specifying valid IDs.

Otherwise you could try other ways of getting the ID:

var ID = $('.activity').first().attr('id');

However I really don't think your issue is with the jQuery snippet you posted. If you supply us with some more context we might be able to help more.

Upvotes: 1

Hussein
Hussein

Reputation: 42808

Make sure you close your DIVs. To get the first div id, You can also do

var ID = $('.activity').eq(0).attr('id');

Upvotes: 1

tvanfosson
tvanfosson

Reputation: 532435

One thing that is wrong is that your ids need to start with a letter. Numeric only ids are not legal HTML. The selector looks ok otherwise, so I'm not sure why you would be getting that error. Are you sure you are including jQuery and no other javascript libraries?

Upvotes: 1

Related Questions