Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript click event doesn't work properly I can't give class

function getCountryByDiv(selectedCountry) {
            x = document.getElementById("neighbors").children;
            console.log(x);
            for (let i of x){
                console.log(i);
                if(i.classList.contains("clicked")){
                    i.classList.remove("clicked");
                }
                else{
                    selectedCountry.classList.add("clicked");
                    console.log("evet");
                }
            }
            htag = document.querySelector(".clicked .card-title");
            console.log(htag);
            getCountry(htag.innerText);
        }

        function renderNeighbors(data) {
            console.log(data);
            let html = "";

            for (let country of data) {
                html += `
                <div onclick="getCountryByDiv(this)" class="col-2 mt-2">
                    <div class="card clickable">
                        <img src="${country.flags.png}" class="card-img-top">
                        <div class="card-body">
                            <h6 class="card-title">${country.name.common}</h6>
                        </div>
                    </div>
                </div>
            `;

            }
            document.querySelector("#neighbors").innerHTML = html;
        }

Click event doesn’t work properly and I don’t know why. When I click to the last child it can’t find innerText. But there is innerText. I can’t see any problem but it still doesn’t work. I can’t give class to the last child (.clicked).

Error](https://i.stack.imgur.com/RFfqy.png)

But it has innerText](https://i.stack.imgur.com/DSCvX.png)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If you want I can send the whole code.

design](https://i.stack.imgur.com/62iVm.png)

I’m trying to give class to the last child and send the innerText of h6 to the function.

(English is not my native tounge so if I wrote anything wrong sorry about that.)

>Solution :

You have to add the "clicked" on the clicked div outside of the loop. You already pass the clicked elem as a parameter, so you can set it directly:

function getCountryByDiv(selectedCountry) {
        x = document.getElementById("neighbors").children;
        console.log(x);
        for (let i of x){
            console.log(i);
            if(i.classList.contains("clicked")){
                i.classList.remove("clicked");
            }
        }
        // moved here
        selectedCountry.classList.add("clicked");
        
        htag = document.querySelector(".clicked .card-title");
        console.log(htag);
        getCountry(htag.innerText);
    }

In the original code it failed on the last element because there was no following element which resetted the class "clicked" in the else-condition.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading