Reputation: 35
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
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
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