Trying to revert image borders to 0 wiht Javascript on click

document.getElementsByTagName('img').style.border = '0';
document.getElementById(“ca1”).style.border=“2px red solid”;
document.getElementById(“ca5”).style.border=“2px red solid”;

I’m using the above in a function. I expect all the images to revert to no border, but nothing happens. Later within this function is setting a border around the selected images. (That works if I remove the top line.) But I want any other image borders to be reset to 0 first.

How do I revert all the images to no border?

Hi there WebSteve,

don’t use CSS in the Javascript. :unhappy:

Instead use …
element.classList.add();
…and…
element.classList.remove();

If you need help with this method,
then supply the actual HTML used.

coothead

1 Like

But in more general terms - getElementsByTagName returns an array-like object; you cannot apply a style (or a class) to such an object. You have to walk through the result of your search and apply it to each in turn.

you can use getElementById or querySelector instead to get a single dom, not a domCollection

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.