JavaScript Tips

This post will be updated with various javascript related tips which are not extensive enough to have their own post.

How to Select any element and change its any property

You can access any element using javascript by calling it through its different attributes, in various ways, like:

document.getElementsByName('yourname')[0]; // adding zero means the first one out of all


document.getElementById('yourname'); // there can be only 1 id per page

And to alter any attribute of this element, smiply use setAttribute function.

E.g. to change the value property of an element with Id ‘myelement’, we can use this code:

document.getElementById('myelement').setAttribute("value", "our new value");

The attribute that we’re trying to set can be anything, any randomly generated attribute name too.

Leave a Reply

Your email address will not be published.