Home >Web Front-end >JS Tutorial >How to Hide Elements (including the Triggering Element) After JavaScript Interaction?

How to Hide Elements (including the Triggering Element) After JavaScript Interaction?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-23 22:26:15123browse

How to Hide Elements (including the Triggering Element) After JavaScript Interaction?

How to Hide Elements after Interaction Using JavaScript

In HTML, elements can be hidden or shown using the display property of CSS. By manipulating this property with JavaScript, developers can toggle the visibility of elements dynamically.

In the provided code snippet, the function showStuff() is triggered when the "Edit" link is clicked. This function unhides the textarea element, which is initially hidden using the display: none; style.

However, the question also asks how to hide both the "Edit" link and the "Lorem ipsum" text. To achieve this, the showStuff() function can be modified as follows:

function showStuff(id, text, btn) {
    document.getElementById(id).style.display = 'block';
    // hide the lorem ipsum text
    document.getElementById(text).style.display = 'none';
    // hide the link
    btn.style.display = 'none';
}

In this updated function:

  1. The id parameter remains the same, representing the ID of the element to be shown.
  2. A new parameter, text, is added to represent the CSS ID of the "Lorem ipsum" text element that should be hidden.
  3. A new parameter, btn, is added to represent the HTML element (the "Edit" link in this case) that should be hidden after being clicked.

In the updated HTML code, the same function is called with additional parameters:

<td>

Now, when the "Edit" link is clicked, not only will the textarea become visible but both the "Lorem ipsum" text and the "Edit" link itself will be hidden. This demonstrates how JavaScript can be used to manipulate the visibility of multiple elements in a single function call.

The above is the detailed content of How to Hide Elements (including the Triggering Element) After JavaScript Interaction?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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