you're gonna get some syntax errors if you use that code!
but for real though that's still a mess. jQuery is miles better
like look at this
document.getElementsByTagName("p").style.color = "blue";
that doesn't make every p element blue. it just results in an error
notably:
document.getElementsByTagName("p");
does not return any elements. just "[]" for some reason
I don't even know how to solve that
although, apparently, the same thing happens with jquery. dunno what's up with that but at least it's still easier to achieve "[]"
all you gotta type is $("p"); to get the same result
Because ElementsByTagName and ElementsByClassName return arrays. To get the first, you'd use
document.getElementsByTagName('p')[0].style.color = "blue";
To apply it to all, you'd use a for loop.
var tags = document.getElementsByTagName('p');
for(i = 0; i < tags.length; i++) {
tags[i].style.color = "blue";
}
Yeah I know it's long, which is one of the reasons why jQuery was made, but having to load up jQuery for small tasks is much slower.
Also it might return [] if there's no p tags. This'll happen if the script is before the element in HTML because the script is ran before the elements are created. Add the script to the end of the page or use
window.onload = function() {
code;
};
i think it goes this way for jQuery
$(document).ready({
code;
});