How to Hide a DIV in JSP HTML


HTML elements can be controlled by server-sided languages using conditional statements. A JSP script using an "if / else" statement can set the display property for the <div> tag before outputting the HTML to the user's computer. The variable value used in the "if" statement is easily passed in the URL to reflect the user's setting.

  • Open the JSP file for editing in your favorite script editor. If you are using a WYSIWYG editor, switch to view the source code.

  • Enter following code for an "if" statement that uses the URL passed variable to control the output:


    String hide = request.getParameter( "hide" );

    if( hide.equals( "true" ) )


    out.println("<a href='mypage.jsp?hide=false'> Show Section </a>");

    out.println("<div id='mydiv' style='display:none;'>");




    out.println("<a href='mypage.jsp?hide=true'> Hide Section </a>");

    out.println("<div id='mydiv' style='display:block;'>");



    The JSP code replaces the opening <div> tag. You need to replace "mypage" with the name of the file and the "div id" with the appropriate reference for your <div> tag.

  • Save the JSP file, then upload it to your Web server and preview it. As you click on the link to hide or show the <div> element, the page will be reloaded from the server with the appropriate display setting to either hide the element or show it as a block.

Tips & Warnings

  • You need to change only the opening <div> tag and the link controlling whether to hide or show the <div>. You do not need to replicate the remaining HTML for the content of the <div> and the closing <div> tag.


  • Photo Credit Comstock/Comstock/Getty Images
Promoted By Zergnet


You May Also Like

Related Searches

Is DIY in your DNA? Become part of our maker community.
Submit Your Work!