Last Updated : Jan 1, 2023
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.
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> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
- The<html> tag is used to indicate the beginning of HTML document.
- 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.
- 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.
- <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
- 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.
- 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.
- 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.
- Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.
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.