Mina Bolis Mina Bolis - 2 months ago 5
Javascript Question

getElementByClassName select all elements with given classes

I have some images with class "myImages". When I select one of the images, that image class name changes to "myImages selected". What am trying to do now is an if statement to test if any of the images are selected and if not, returns an alert box. It would look something like this (Syntax not correct):

function alertBox() {
if document.getElementByClassName("myImages")[all elements of class myImages] contains class name ("myImages selected"):
alert("Please select an image");
}

Answer
function alertBox() {
  if (document.getElementsByClassName('myImages selected').length === 0) {
    alert('Please select an image');
  }
}