0

If the style attribute is already set to display: none; how can I change it to be display:block; when calling the toggleControls function?

function toggleControls() {
    var leControls = document.getElementsByClassName('le-controls'); 
    for(var i = 0; i < leControls.length; i++) { leControls[i].style.display = 'none'; }
}

1 Answer 1

2

You can just check if it's already none:

var style, leControls = document.getElementsByClassName('le-controls');
for (var i = 0; i < leControls.length; i++) {
    if ('none' == leControls[i].style.display) {
        style = 'block';
    }
    else {
        style = 'none';
    }
    leControls[i].style.display = style;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks you. Would you mine explaining what's going on there, or putting that into context with the rest of the function?
@rick I added back the snipped parts of the function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.