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 h1 tag for display heading, div element with some codes of styles and two buttons defined ‘None, Hide’ with id’s ‘dis_n,vis_h’ for trigger click event.
In script when user clicks on ‘None, Hide’ any buttons which triggers click event by ‘addEventListener’ then it loads function.
There we collecting div element class ‘b’ by getElementsByClassName() method then we sets style properties of display or visibility with respective values ‘None, Hidden’ and it will hides element.
- <!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 h1 tag with ‘Hide Elements By Class Name’ heading, div element with some lines of style by using class name ‘b’ then we defined ‘None, Hide’ buttons with separate id’s ‘dis_n,vis_h’ for access those elements.
- In script we referred each buttons by specifing their id’s in getElementById() method then which is appends with ‘addEventListener()’ for listen when click event occur.
- Within that we collecting div element by class name ‘b’ then we sets display property to ‘none’ for hide state and likewise we sets ‘b’ visibility property to ‘hidden’ for hide state.
- Here must we need to do another thing is nearby getElementsByClassName() method we have specify  for work this concept correctly otherwise we can’t get the result because it is defined for handle array.
- If you handle this as like getElementById() method which can’t returns anything because getElementById() method defined with other behavior so we can easily access it.
- 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 heading, div section with some styles and two buttons None, Hide if user clicks on ‘None’ then div will disappeared like ‘Hide’ also do the same.
If user things to hide other element then define them then use this concept get successful result.