I want to get the first element that has style.fontWeight = 'bold';
Heres what i tried
function navigate(input) {
var array = document.querySelectorAll('.menuulli').style.fontWeight == 'bold';
console.log(array)
}
Since bolder, bold, normal all maps to their corresponding numerical values for the weight of the fonts you can try the following way:
function navigate(){
var els = document.querySelectorAll('.menuulli');
els = [...els].filter(el => {
return getComputedStyle(el).fontWeight == "700"
})[0];
return els;
}
console.log(navigate());
.menuulli{
font-weight: bold;
}
<ul>
<li class="menuulli first">1</li>
<li class="menuulli">2</li>
</ul>
bold can actually be internally converted to number, kudos!Use querySelectorAll with the attribute selector:
const array = document.querySelectorAll('.menuulli[style="font-weight: bold;"]');
console.log(array);
If you have a more than one style declaration on your HTML tag.
For example: <div class"menuulli" style="color: red; font-weight: bold;">
Then use this selector:
/// note the asterisk
const arr = document.querySelectorAll('.menuulli[style*="font-weight: bold;"]');
Here is a JSFiddle example: jsfiddle.net/Konrud/g7vzc9s5
querySelectorAll returns a NodeList so you cant check for styles on that but you have to check on items.
function navigate(input) {
const elems = document.querySelectorAll('.menuulli');
const elemsArray = Array.from(elems);
array = elemsArray.map((ele)=>
ele.style.fontWeight === "bold");
}
<div style="font-weight: bold">) ?document.querySelectorAll('.menuulli[style="font-weight: bold;"]')var array = document.querySelectorAll('.menuulli[style="font-weight: bold;"]'); console.log(array)It just gives me "NodeList []" in the console. And there is an element that has this css property.