With jquery, I've got the following code:
$('a[data-hello]').click(function(){ = That select all "a" elements with "data-hello".
I'm trying to make this with raw Javascript. I stop here:
document.querySelectorAll("data-hello").onclick = function() {
(btw, theres a way to select all the A elements with data-hello and not all with data-hello? o.O)
But querySelectorAll returns a Array. Because of this, it only works if I determine a position. This way:
document.querySelectorAll("data-hello")[5].onclick = function() {
But I want ALL ELEMENTS, not specific elements, like with jQuery. I cant use jQuery.
It is so simple with Jquery :( I must make a "for" to wade through all the positions in JS? Is this necessary? sorry I do not understand...
What I want to do:
I want to get the data attribute value of the element that is clicked. I use this for this inside the function and, then, I applied another function that add a class in a specific element.
Basically, there is buttons with classes in data attribute value. This classes will be applied to a specific element.
querySelectorAllreturns a Array." - No it doesn't, it returns a NodeList. (Which yes, you would then have to loop through - which is what jQuery methods like.click()do behind the scenes.).pop(),.forEach(), etc. (A NodeList is an array-like object, so you can use some array methods viaArray.prototypeand.call()or.apply().)NodeListisn't a fully-fledged Array, which caught me out. This Mozilla Developer Network (MDN) article explains it very well. Btw, googlingmdn <html|css|javascript term>is a great way to begin debugging - I do so thousands of times a day =)