[Solved] Hide and Show Div with button [duplicate]


You can use HTMLelement.addEventListener to handle button click event and hide or show the div appropriately using the element.style.display property, the code below demonstrates how it works

const divE = document.getElementById('more');
const btn = document.getElementById('show');


handler = () => {
    if (divE.style.display != 'none') {
        // if the element is not hidden, hide the element and change button text to "more"
        btn.innerHTML = 'More';
        divE.style.display = 'none';
    } else {
        // if the element is hidden, show the element and change button text to "less"
        btn.innerHTML = 'Less';
        divE.style.display = 'block'
    }
}

btn.addEventListener('click', handler)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button id="show">Less</button>
    <div id="more">
        IDk something here ig
    </div>
</body>
</html>

10

solved Hide and Show Div with button [duplicate]