how to select an element by class in JavaScript with getElementsByClassName and set its width without jquery

Practice




how to select an element by class in JavaScript with getElementsByClassName and set its width without jquery

getElementsByClassName('classname')
does not work

you probably need to use it before the dom has loaded?
as I understand it, but then there is a chance that the element does not exist yet,
whereas if it has already loaded, you can perfectly well use jquery

but if you want to do it in pure JavaScript
then you can do it like this

var elems = document.getElementsByClassName('myclassname'); //getElementById("myid");
for (var i = 0; i {
elems[i].style.width = 1000+ 'px';
}

Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Scripting client side JavaScript, jqvery, BackBone"

Terms: Scripting client side JavaScript, jqvery, BackBone