Question

Let say I have my HTML code look like this:

<p class="p">Paragraph</p>
<p class="p p2">Paragraph 2</p>
<p class="p p3">Paragraph 3</p>

If I use: $('.p').css('color','red'), this will apply red color to all paragraphs.

How can I apply the style to paragraph that only has class p which is first paragraph in this case?

Was it helpful?

Solution 2

You can use:

$('.p[class="p"]').css('color', 'red');

or:

$("p[class='p']").css('color', 'red');

Fiddle

OTHER TIPS

You can use attribute selector:

$('p[class="p"]').css('color','red');

http://jsfiddle.net/umxGh/

Or:

$('p').filter(function(){
   return this.className === 'p';
}).css('color', 'red');

Use an attribute selector:

$('p[class="p"]').css('color','red')

Try this:

$('[class=p]').css('color','red');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top