FILIaS
FILIaS

Reputation: 505

Hide a '<div>' depending on a javabean value

As the title describes, I want to hide a div if the bean returns a specific value. I have the following code, but doesn't work (it doesn't hide the div)

JSP:

<jsp:useBean id="Product1" type="Model.Product" scope="request" /> 
    <script>       
            if(  "empty_val".equals(${Product1.name}))
            {   
                product1.style.display = "none";
            }
    </script>

    <div id="product1">

...

Servlet sends everything right but I cannot find the way to 'handle' the occasion of a specific bean value in order to hide the 'div'. Any ideas? Thank you in advance.

Upvotes: 1

Views: 3135

Answers (3)

Matt
Matt

Reputation: 4775

If your bean already exists in a scope, then eliminate the <jsp:useBean /> tag.

If your intention is to write the div to the page, but to set its display style to none:

<div id="product1">...</div>
<script>
<c:if test="${Product1.name eq 'empty_val'}">
    product1.style.display = "none";
</c:if>
</script>
...

If you would rather just not write the div to the page:

<c:if test="${Product1.name ne 'empty_val'}">
    <div id="product1">...</div>
</c:if>

Upvotes: 2

Jigar Joshi
Jigar Joshi

Reputation: 240870

Better to use <c:if> with JSTL and display the DIV conditionally

<c:if test="${Product1.name eq 'empty_val'}">
 <DIV></DIV>
</c:if>

See

Upvotes: 2

karim79
karim79

Reputation: 342635

// JavaScript ain't Java
if ("${Product1.name}" === "empty_val") {
    ...
}

...better to add a class (e.g. "hide") to the div depending on the server value, there's really no need for JavaScript:

.hide {
    display: none;
}

Upvotes: 0

Related Questions