Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
133
rated 0 times [  139] [ 6]  / answers: 1 / hits: 29307  / 11 Years ago, sat, september 21, 2013, 12:00:00

What is the pure equivalent of jquery's eq(). For example, how may I achieve



$(.class1.class2).eq(0).text(1254);


in pure javascript?


More From » jquery

 Answers
13

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;



  • In your example 1254 is a number, if you have a string you should use = 'string'; with quotes.

  • If you are only looking for one/the first element you can use just .querySelector() insteal of .querySelectorAll().



Demo here



More reading:




MDN: textContent

MDN: innerHTML

MDN: querySelectorAll



[#75546] Friday, September 20, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
terrellhunterm

Total Points: 82
Total Questions: 109
Total Answers: 98

Location: Vietnam
Member since Sun, Oct 18, 2020
4 Years ago
;