Unveiling a hidden div when a JSP expression language is not empty

Is there a proper way to display a hidden div only when certain conditions are met? For example, I want to show a login error message within a div only if there is an error.

<div id= "error" class="alert alert-danger" hidden="hidden" role="alert">
    ${loginError} 
</div>

I attempted to use a script to achieve this but it was unsuccessful.

<script type="text/javascript">
    if(null != request.getAttribute("loginError")){
        document.getElementById("error").removeAttribute("hidden");

    }
</script>

Answer №1

To truly understand the essence, it is crucial to recognize the precise moments when each code segment is executed - JSP runs on the server side, prior to delivering the page content to the browser. In contrast, Javascript operates within the browser environment, processing functions post-receiving the pre-generated response. Consequently, Javascript lacks a request variable, rendering statements like:

if(null != request.getAttribute("loginError")){

illogical. An alternate and more refined approach would be:

<div id= "error" class="alert alert-danger" ${ empty loginError ? '' : 'hidden'} role="alert">

(The exact value of hidden attribute is irrelevant, as its mere presence dictates hiding the respective element.)

Answer №2

To alter the visibility of an HTML element, it is necessary to modify the hidden attribute value to false. Even an empty string will automatically add the hidden attribute to the element. Therefore, utilizing boolean values is essential when changing the state of hiddenness in an HTML element.

document.getElementById("error").hidden = false;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I adjust height without affecting weight in CSS using curly brackets?

Can CSS be used to specifically adjust the height of a curly bracket without changing its thickness? I have been attempting to modify the height of a curly bracket dynamically by adjusting the font-size, but this also alters the weight. Is there a workar ...

Replacing one <div> with another <div> using a clickable link within the divs

In one of my web pages, there is a div that I'll refer to as div1. Within this div, there is a link called 'Link1'. My goal is to click on Link1, triggering the replacement of div1 with div2. Inside div2 there will be another link, let&apos ...

Is there a method to trigger click events on overflow content when clicked?

I am currently tackling a significant issue: I need the drop-down options' width to be wider than where the selected value is displayed, especially in IE7. After conducting some research on Google, I decided to take matters into my own hands and write ...

The container struggles to contain the excess of images spilling over

I'm having trouble getting a group of images to stay within their container in a grid layout, as they're overflowing vertically beyond the container's boundaries. Is there a way to adjust their size so they match the height of their parent ...

My DIV is not floating to the right as expected. What could be the issue?

I'm having trouble positioning the timer to float on the right top side of the logo. Even though using the regular style="float:right" works perfectly... <div id="whole_page" /> <div id="header" /> <?php echo " ...

Cover the entire screen with numerous DIV elements

Situation: I am currently tackling a web design project that involves filling the entire screen with 60px x 60px DIVs. These DIVs act as tiles on a virtual wall, each changing color randomly when hovered over. Issue: The challenge arises when the monitor ...

Bootstrap3 and jQuery are attempting to achieve vertical alignment

I am trying to vertically align Bootstrap col-md* columns. I have two blocks, one with text and one with an image, and I want them to be equal in height with the text block vertically centered. My current solution is not working correctly. Can someone plea ...

What is the best way to position an SVG background image at the bottom of a container?

I am facing an issue with using an SVG as a background-image on a pseudo element. The image is not as tall as the container, so I would like to position it at the bottom of the container while having the background color fill up the top portion to create a ...

``Despite setting display block, margin auto is not being adhered to as expected

Trying to position two label elements on either side of a div container has been tricky. Despite using various display properties like display: block and display: inline-block, the margins are not behaving as expected. div { color: #ffffff; ...

Tips for arranging cards responsively in a vertical column

I am working on creating simple card items using HTML and CSS. I have successfully aligned the cards in a row on a PC, but I would like them to be displayed vertically in a column on smaller screens. Below is the code I am using. body { width: 100vw ...

Newly designed Bootstrap 4 input field with search feature positioned off-center on smaller screens

I have successfully positioned an input text next to a button as shown below: While it displays perfectly on a regular computer screen, the alignment gets skewed when viewed on a phone device: This is the functional code I am using: <!-- Add Filer Fo ...

Locator for finding compound text within a div class using Selenium WebDriver

I am struggling to select a specific button in the UI that shares similarities with other elements. Below is the code snippet for the button in question: <div class="ui green ok inverted button"> <i class="checkmark icon"></i> Yes </d ...

Resolving the Material-UI NextJS Button Styling Dilemma

I've encountered an issue with the styling of a few buttons in JS. When I apply a styling class using className, the formatting works fine on the initial render but loses its styling on subsequent refreshes. This problem is specific to two individual ...

Ineffective Implementation of While Loop for Div Visibility

Here's a simple code that generates divs using a while loop. However, it doesn't seem to be working as expected. It's actually just basic HTML, nothing too complex. But for some reason, it's not displaying any of the five divs. <? ...

Pointer-events property in pseudo elements not functioning as expected in Firefox browser

I have a horizontal menu that includes mid-dots (inserted using the ::after pseudo element) to separate the menu items. I want to prevent these mid-dots from being clickable. This works fine in Safari, but in Firefox (v.47), the pointer-events: none prope ...

Struggling to use GSAP to control the timeline with my custom functions

I'm attempting to create a greensock animated banner with the ability to switch between different scenes. I've made progress by setting up the timeline for each scene's intro and outro animations using the GreenSock API documentation. Howeve ...

I'm currently developing a Chrome application specifically designed for editing plain text. To achieve this, I am utilizing the <textarea> element. However, the app will not expand to full screen

Currently, I am in the process of developing a Chrome app and have implemented CSS from a previous post. The main issue I am facing is with the textarea element that I am using. I am open to exploring alternative solutions to achieve the desired outcome. S ...

Is it possible to conceal my Sticky Div in MUI5 once I've scrolled to the bottom of the parent div?

Sample link to see the demonstration: https://stackblitz.com/edit/react-5xt9r5?file=demo.tsx I am looking for a way to conceal a fixed div once I reach the bottom of its parent container while scrolling down. Below is a snippet illustrating how I struct ...

Bootstrap: Organize Columns Simultaneously with Push and Pull for Customized Column Ordering

I am experiencing an issue with the concurrent use of "Push" and "Pull" column ordering classes. I would like my columns to be ordered like this: However, the result on medium and larger screen sizes is <section style="direction:rtl;"> <div cla ...

Experiencing an issue while rendering due to the presence of display: table-cell;

I am currently in the process of developing a website. The HTML Header section is structured as follows: <div id="page"> <header> <a href="#" class="redsquare">&nbsp;</a> <div class="head-wrapper"> ...