All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

JavaScript Hide Element

Last Updated : Mar 11, 2024

JavaScript Hide Element

In this article we will show you the solution of JavaScript hide element, the style.display or the style.visibility methods in JavaScript can be used to hide elements.

An element can also be hidden in JavaScript by using the visibility attribute.

When employing visibility, the style.visibility differs from the style.display in the following ways: The tag is hidden, so it cannot be seen, but there is still space.

The tag is also hidden when using display: none, but no extra room has been reserved on the page. Now move to the concept of javascript hide element.

Step By Step Guide On JavaScript Hide Element :-

The hidden attribute in HTML can be used to cover up a specific element.

Elements are either hidden or visible depending on the value of the HTML hidden attribute, which defaults to true when set to true.

By utilising JavaScript/jQuery to access the DOM element, the style display attribute is utilised to hide and show the content of the HTML DOM.

By setting the style display attribute to "none," you can conceal an element.


document.getElementById("element").style.display = "none";
<!DOCTYPE html>
Javascript hide element
Welcome to the Talkerscode
Example of the Javascript hide element..
<div id = "div" style = "background-color: grey; font-size: 25px; color: white; border: 2px solid white;">
Welcome to the Talkerstech..
<p id = "p"> Welcome to the Talkersmoney.. </p>
<button onclick = "fun()" id = "btn">
Hide me!
function fun() {
document.getElementById("div").style.display = "none";
document.getElementById("p").style.display = "none";
  1. Our code begins with <!DOCTYPE html>. It is a statement that is shown above all other components on a web page.
  2. Every HTML document must contain the document type declaration in accordance with the HTML specification and standards in order to guarantee that the webpages are displayed as intended.
  3. The title of the web page is then provided using the HTML and HEAD tags with the aid of the TITLE element.
  4. The TITLE and HEAD tags are where we end up. It uses the BODY tag to begin the body of the code.
  5. The heading is written in the body by using H1 tag. The H2 tag is then used to create a new heading.
  6. Next, we utilise the paragraph element that is hidden when the supplied button is clicked, along with a div element that provides styling for the background colour, border colour, and font size.
  7. Next, we create a button with the function using onclick event value and its unique ID.
  8. We design the procedure that launches an action when a user hits a scripted button.
  9. After that, we utilise the style.display method, which has the value of none, to hide an element by its id.
  10. To see the result, click the "Hide me!" button. Then to finish, we add SCRIPT, BODY, and HTML tags to our code.

Conclusion :-

Thus, we have successfully acquired knowledge of the javascript hidden element.'

In addition, we discovered that a specific element can be hidden using the HTML hidden attribute.

The HTML hidden attribute's value, which defaults set true when set to true, determines whether an element is hidden or visible.

I hope this article on JavaScript hide element helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪