1

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");
}
Mina Messiah
  • 119
  • 6
  • 2
    `document.getElementsByClassName('myImages selected').length === 0`. Or `document.querySelectorAll('.myImages.selected').length === 0`. The [docs](https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName) are fairly clear here: they say "`names` is a string representing the **list of class names to match; class names are separated by whitespace**". –  Sep 20 '16 at 05:08

1 Answers1

1
function alertBox() {
  if (document.getElementsByClassName('myImages selected').length === 0) {
    alert('Please select an image');
  }
}
fvgs
  • 21,412
  • 9
  • 33
  • 48