All TalkersCode Topics

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

Hide And Show In JavaScript

Last Updated : Mar 11, 2024

Hide And Show In JavaScript

In this tutorial we will show you the solution of hide and show in JavaScript, here we are going to show you example by using display and visibility property. The display property is used to hide and show the contents of HTML DOM using JavaScript.

If we want to hide the element set the style display property to ‘none’ otherwise we can set it as ‘block’ which default property value also for elements.

The visibility property also same as above said we can use this for both hide and show html elements.

Step By Step Guide On Hide And Show In JavaScript :-

Here we defined div elements with some codes of styles and four buttons defined ‘Display, None, Visible, Hide’ with id’s ‘dis_d,dis_n,vis_v,vis_h’ for trigger click event.

In script when user clicks on ‘Display, None, Visible, Hide’ any buttons which triggers click event by ‘addEventListener’ then it loads function.

There we collecting div element id ‘b’ by getElementById() method then we sets style property as display or visibility with respective values ‘None or Block, Visible or Hidden’.

It will either shows or hides every elements based on property value.

<!DOCTYPE html>
        <title>HIDE & SHOW</title>
                padding: 2rem;
                border: 5px solid rgb(18, 220, 89);
                width: max-content;
        <div id="b">You can hide/show this box using javascript</div>
        <button id="dis_b">Display</button>
        <button id="dis_n">None</button>
        <button id="vis_v">Visible</button>
        <button id="vis_h">Hide</button>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. Here we defined div element with some lines of style by using id ‘b’ then we defined ‘Display, None, Visible, Hide’ buttons with separate id’s ‘dis_d,dis_n,vis_v,vis_h’ for access those elements.
  7. In script we referred each buttons by specifing their id’s in getElementById() method with then which is appends with ‘addEventListener()’ for listen when click event occur.
  8. Within that we collecting div element by id ‘b’ then we sets display property to ‘none’ for hide state and ‘block’ for shows state. Then likewise we sets ‘b’ visibility property to ‘visible’ for show state and ‘hidden’ for hide state.
  9. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to define hide and show in javascript.

When we executes program on browser we can see div section with some styles and four buttons Display, None, Visible, Hidden if user clicks on ‘None’ then div will disappeared like ‘Hide’ also do the same and if user clicks on ‘Display’ or ‘Visible’ button then div will appeared.

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

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪