What is the pure equivalent of jquery's eq()
. For example, how may I achieve
$(".class1.class2").eq(0).text(1254);
in pure javascript?
To get the element index in the array you can use []
in javascript. So to reproduce your code you can use this:
document.querySelectorAll('.class1.class2')[0].textContent = 1254;
or
document.querySelectorAll('.class1.class2')[0].innerHTML = 1254;
1254
is a number, if you have a string you should use = 'string';
with quotes. .querySelector()
insteal of .querySelectorAll()
.Demo here
More reading:
MDN: textContent
MDN: innerHTML
MDN: querySelectorAll
querySelectorAll
returns an array, so you can get the element 0
using index
document.querySelectorAll(".class1.class2")[0].innerHTML = 1254
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