Link to home
Start Free TrialLog in
Avatar of Aaron Roessler
Aaron Roessler

asked on

Jquery: How to get the first element on page from an array?

I am using jQuery to add a Class to the first <p> selector within a div wrapper with a class .addline using this:
jQuery( ".addline p" ).first().addClass( "leftline" );

This works great and adds that class to only the first <p class="leftline"> within <div class="addline">

but sometimes <p> is not the first selector I want to add that class to. So I need jQuery to only look for the first of one of these: var ary = ['p', 'h2', '', 'h3', 'li', 'ol'];

I thought maybe I could do something like this:

var ary = [$p_selector, $h2_selector, $h3_selector, $li_selector, $ol_selector];

var $p_selector =  jQuery( ".addline p" ).first();
var $h2_selector =  jQuery( ".addline h2" ).first();
var $h3_selector =  jQuery( ".addline h3" ).first();
var $li_selector =  jQuery( ".addline li" ).first();
var $ol_selector =  jQuery( ".addline ol" ).first();

Then use an IF statement or something.?

Here is a video to help explain.
https://www.loom.com/share/27fd9940e39c4bcc8c30a65965dc3c2c

Sample Page: https://allgood.flywheelsites.com/all-goods-comprehensive-guide-to-non-toxic-natural-remedies-for-eczema/
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Aaron Roessler
Aaron Roessler

ASKER

I removed the unrecognized expression in the ary. ' ' and then it worked perfectly!!  Thanks!!
you welcome!