user2169140
user2169140

Reputation: 35

select an unknown class in jquery

we write this code in Js for select an class(whitn't a class or Id name):

var x=document.getElementsByTagName("p");
x[2].style.color="red"

Please note x! But how we do it in jquery?!

Upvotes: 0

Views: 311

Answers (3)

Hemant Barhate
Hemant Barhate

Reputation: 5

Try this :

$("p").eq(2).css("color","red");

Upvotes: 0

bipen
bipen

Reputation: 36531

try this

$('p').eq(2).css({color:red})

$('p') selects all <p> elements.
.eq(2) selects the third <p> element.
.css({color:red}) adds the styles to the selected p tag

Upvotes: 1

GautamD31
GautamD31

Reputation: 28763

Try with .css() with selecting .eq() to choose like this

$(document).ready(function(){
    $("p:eq(2)").css("color","red");
   //OR
    $("p").eq(2).css("color","red");
});

Consider that x[2] will select the 3rd 'p' tag as eq(2) do.

Upvotes: 2

Related Questions