I'm using (trying to use) jQuery to add a class to the first paragraph of every div with class of ".djBio" My problem is that it's only adding the class to the first div.djBio, not all divs with that class. Here's the code. I am also using the fancy letter jquery plugin to add the drop cap to the first paragraph (which also is only applying to the first div.djBio, not all)
jQuery(function( $ ){
$('.servicesContent p:first, .about-usContent p:first, .djBio p:first').fancyletter().addClass('firstP');
});
Thanks so much for your help!
The addClass() method adds one or more class names to the selected elements. This method does not remove existing class attributes, it only adds one or more class names to the class attribute. Tip: To add more than one class, separate the class names with spaces.
Approach: First select the element to which multiple classes will be added. Then use addClass() method to add multiple classes to the element and removeClass() method to remove multiple classes.
You've stumbled over what everyone stumbles over at some point: :first
doesn't really mean what you (at first) think it means. :first
will only ever return (at most) one element. The best way I can think of doing your problem is using each()
:
$(function() {
$(".servicesContent, .about-usContent, .djBio").each(function() {
$(this).find("p:first").addClass("...");
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With