Home > Web Front-end > JS Tutorial > How Can JavaScript Dynamically Hide and Show HTML Elements?

How Can JavaScript Dynamically Hide and Show HTML Elements?

Linda Hamilton
Release: 2024-12-15 21:02:29
Original
664 people have browsed it

How Can JavaScript Dynamically Hide and Show HTML Elements?

Hiding Elements Dynamically using JavaScript

Suppose you have a button that allows users to edit a text element. After clicking the button, you may want the button to disappear and the text element to be replaced with a text area for editing. Here's how you can achieve this using JavaScript:

function showStuff(id, text, btn) {
    // Display the element with the given ID
    document.getElementById(id).style.display = 'block';

    // Hide the lorem ipsum text
    document.getElementById(text).style.display = 'none';

    // Hide the link
    btn.style.display = 'none';
}
Copy after login

In the provided HTML snippet, here's how you can modify it:

<td class="post">

    <a href="#" onclick="showStuff('answer1', 'text1', this); return false;">Edit</a>
    <span>
Copy after login

When the "Edit" link is clicked, the following actions occur:

  • The hidden "answer1" span, containing the text area, becomes visible.
  • The "text1" span, containing the lorem ipsum text, is hidden.
  • The "Edit" link itself becomes invisible.

The above is the detailed content of How Can JavaScript Dynamically Hide and Show HTML Elements?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template