javascript – Comparing user input text to dataset value and add class if it's true-ThrowExceptions

Exception or error:

i’ve got a few img elements with dataset and i have to add class “invisible” to img element if user input text is equal to one of value in dataset.

This is my code to hide button:

hideButton.addEventListener('click', function() {
    let text = inputPlace.value;

    for (let i = 0; i < allImages.length; i++) {
        let allDataset1 = [];
        allDataset1.push(allImages[i].dataset.tag.split(','));
        allDataset1 = allDataset1[0];   
        for (let j = 0; j <= allDataset1.length; j++) {
            if (allDataset1[j] === text) {

            }
    }


     }
    inputPlace.value = "";

Now i don’t know how to do “if” i compared it and it works but how to add class to exactly this element which contains dataset.

For example: there are a img elements with dataset: “baseball, sport, people”, next element with dataset: “nightlife, people, bike” etc. and user’s input text is “people” so i have to add class “invisible” to this two elements.

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *