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
How to Hide an Object with CSS
HTML elements can be hidden in CSS using either of two options depending on the effect you desire. If you want an...
How to Reference a CSS File From JSP
One of the benefits of Java Server Pages (JSP) is that you can let your Web server do some of the work...
How to Hide a Link on a Web Page
Hidden links are typically used on websites for what are sometimes referred to as "Easter eggs." Easter eggs reward regular visitors to...
How to Create a DIV With PHP
The PHP scripting language enables you to add interactivity to your HTML. PHP enables you to combine PHP coding with HTML coding...
How to Hide a DIV Tag
In HTML, "<div>" tags are used to separate out blocks of text, giving them unique styles or carefully positioning them on a...