简体   繁体   中英

Loop elements and check if class exists javascript

I have 3 select option. I can get them in this way:

var theSelect = $("#id_"+portletId+"_ .selectpicker");

and the results is:

0:select#_Name_zeroid.form-control.selectpicker.completed
1:select#_Name_firstid.form-control.selectpicker.completed
2:select#_Name_secondid.form-control.selectpicker

as you can see 2 of these select have the attributes completed . How can I loop these and get a simple console.log() if the attribute completed exists?

You can use the classList DOM attribute which has a contains method which takes a string and returns a boolean.

Or you can use jQuery

Native

document.querySelectorAll(".selectpicker").forEach(node => node.classList.contains("completed"));

jQuery

// filter the results
const theSelect = $("#id_"+portletId+"_ .selectpicker").filter(".completed"); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM