mercredi 15 juin 2016

How do I write an if statement to check if a style attribute contains nothing?

I'm trying to make a functionality where if I click a span that is not struck through, it will become so and vice versa. I tried making an if statement that would give the exact value for the attribute the is returned when using console.dir(x). In the case of textDecoration, I get textDecoration:"". How would I be able to reflect this in an id statement?

var x = document.querySelectorAll("span")

for(var i=0; i<x.length;i++){

    x[i].addEventListener("click", function(){

        if(this.style.textDecoration=""){

            this.style.textDecoration="line-through"
        }
        else{

            this.style.textDecoration=""
        }
})
}

Edit: As it stands when I click the span, nothing happens.

Aucun commentaire:

Enregistrer un commentaire