David G
David G

Reputation: 96810

How do I obtain a particular element using its tag name?

I'm using $('div').css('color', 'red'); but I want the 5th div in my code to have red text. How would I do this?

Upvotes: 0

Views: 68

Answers (4)

RobG
RobG

Reputation: 147373

POJS:

document.getElementsByTagName('div')[5].style.color = 'red';

It might be boring, but it's 20 to 30 times faster than the jQuery equivalent in IE 8.

Upvotes: 0

icyrock.com
icyrock.com

Reputation: 28598

You can use the :eq jQuery selector. See jsFiddle here:

Also see this SOq:

Upvotes: 0

John Pick
John Pick

Reputation: 5650

To answer the title question, if the particular element is named foo, you would use $('#foo').

Upvotes: 0

Vigrond
Vigrond

Reputation: 8198

$('div:eq(4)').css('color', 'red')

the :eq selector takes a 0-based integer

http://api.jquery.com/eq/

Upvotes: 4

Related Questions