In this tutorial, we are going to see how to create toggle effect using Javascript without jQuery. In the previous tutorial, we have seen about toggling HTML using jQuery.
This example code is preferable when the user don’t want to load the entire jQuery library for toggling HTML.
In this example we are using the same HTML we have seen in the previous tutorial, Toggle HTML with jQuery.
This code is for toggling HTML by changing the display of the selectors.
function toggleText() {
if(document.getElementById("more-option").style.display == 'none') {
document.getElementById("more-option").style.display = '';
document.getElementById("more").style.display = 'none';
document.getElementById("less").style.display = '';
} else {
document.getElementById("more-option").style.display = 'none';
document.getElementById("more").style.display = '';
document.getElementById("less").style.display = 'none';
}
}