I have a simple HTML page with different elements which are being hidden or displayed because of choosing some options before.
I have different options (radiobuttons) which have to be clicked before, they should control and hide or show the elements:
- Household or Nohousehold (Household = 1, Nohousehold = 2)
- Household or Nohousehold and Monument (Monument = 1 or 2)
- Household and Monument
- Nohousehold
- Nohousehold and Zones (Zones = 1 or 2)
- Nohousehold and Monument
- Nohousehold and Zones and Monument
I called the div classes on html after the numbers above (for example: 1 = class1, 2 = class2,.. and so on), each class should be visible by selecting from the options above. This is my edit_outerwalls.jsp which is included in edit.jsp.
<div class="class1"> //This should be displayed only if Household = 1 or 2
...
</div>
<div class="class2"> //This should be displayed only if Household = 1 or 2 and Monument = 1
...
</div>
<div class="class3"> //This should be displayed only if Household = 1 and Monument = 1
...
</div>
<div class="class4"> //This should be displayed only if Household = 2
...
</div>
<div class="class5"> //This should be displayed only if Household = 2 and Zones = 1
...
</div>
<div class="class6"> //This should be displayed only if Household = 2 and Monument = 1
...
</div>
<div class="class7"> //This should be displayed only if Household = 2 and Zones = 1 and Monument = 1
...
</div>
<div class="class2">
...
</div>
<div class="class5">
...
</div>
My jQuery Code looks like this:
function checkClass1234() {
let household = $('input:radio[name="household"]:checked').val(); //this are the radiobuttons on the html page
let zones = $('input:radio[name="zones"]:checked').val();
let monument = $('input:radio[name="monument"]:checked').val();
if (household === '1' || household === '2') {
$('.class1').show();
} else if ((household === '1' || household === '2') && (monument === '1')) {
$('.class2').show();
} else {
$('.class1').hide();
clearValues('.class1');
$('.class2').hide();
clearValues('.class2');
}
if (household === '1' && monument === '1') {
$('.class3').show();
$('.class2').hide();
clearValues('.class2');
} else {
$('.class3').hide();
clearValues('.class3');
}
if (household === '2') {
$('.class4').show();
$('.class3').hide();
clearValues('.class3');
} else {
$('.class4').hide();
clearValues('.class4');
}
if (household === '2' && zones === '1' && monument === '1') {
$('.class7').show();
$('.class2').hide();
clearValues('.class2');
$('.class3').hide();
clearValues('.class3');
} else if (household === '2' && monument === '1') {
$('.class6').show();
$('.class2').hide();
clearValues('.class2');
$('.class3').hide();
clearValues('.class3');
} else if (household === '2' && zones === '1') {
$('.class5').show();
$('.class2').hide();
clearValues('.class2');
$('.class3').hide();
clearValues('.class3');
} else {
$('.class4').hide();
clearValues('.class4');
$('.class5').hide();
clearValues('.class5');
$('.class6').hide();
clearValues('.class6');
$('.class7').hide();
clearValues('.class7');
}
}
My edit.jsp looks like this (in here i include all my pages):
<%@page contentType="text/html" pageEncoding="UTF-8" %>
<jsp:directive.include file="/WEB-INF/sitemesh-decorators/include.jsp"/>
<fmt:setBundle basename="bundles.${orderName}-resources"/>
<c:set var="order" value="${order}" scope="request"/>
<head>
<!-- All Scripts -->
</head>
<jsp:include page="/WEB-INF/jspf/crumb1to4/edit.jsp"/>
<section id="content">
<header>
<h1><fmt:message key="title"/></h1>
<h3>
<fmt:bundle basename="bundles.household-resources">
<fmt:message key="title.comment"/>
</fmt:bundle>
</h3>
</header>
<jsp:include page="/WEB-INF/jspf/edit/mandatory.jsp"/>
<spring:url var="attachmentsUrl" value="/${orderName}/attachments"/>
<form:form method="post" modelAttribute="order" action="${attachmentsUrl}">
<jsp:include page="/WEB-INF/jspf/edit/errors.jsp"/>
<jsp:include page="edit_consultant.jsp"/>
<jsp:include page="edit_household.jsp"/>
<fieldset>
<fmt:bundle basename="bundles.order-resources">
<legend>
<jsp:include page="/WEB-INF/jspf/edit_elem/checkbox.jsp">
<jsp:param name="label" value="caption.buildingshell"/>
<jsp:param name="field" value="buildingshell"/>
<jsp:param name="bundle" value="order"/>
<jsp:param name="tooltip" value="buildingshell.info"/>
</jsp:include>
</legend>
<div id="blockBuildingshell">
<div class="haupttext">
<fmt:message key="buildingshell.comment"/>
</div>
<jsp:include page="edit_outerwalls.jsp"/> <!-- In here i have all the elements with class1, class2, class3... -->
</div>
</fmt:bundle>
</fieldset>
<jsp:include page="edit_machines.jsp"/>
<!-- ...Another Included JSP Pages... -->
<!-- include next button -->
<jsp:include page="/WEB-INF/jspf/edit/next2.jsp">
<jsp:param name="next">
<jsp:attribute name="value">
<fmt:message key="next.comment"/>
</jsp:attribute>
</jsp:param>
</jsp:include>
</form:form>
</section>
And my Household.jsp looks like this (here are the 3 RadioButtons):
<%@ page pageEncoding="UTF-8" %>
<jsp:directive.include file="/WEB-INF/sitemesh-decorators/include.jsp"/>
<fmt:bundle basename="bundles.order-resources">
<!-- Household -->
<fieldset>
<legend>
<fmt:message key="caption.household"/>
</legend>
<div class="formclear"></div>
<h3>
<fmt:message key="caption.location"/>
</h3>
<div class="label">
<form:label path="household.type">
<fmt:message key="household.type"/>
</form:label>
<span class="formAsterics">*</span>
<jsp:include page="/WEB-INF/jspf/edit/tooltip.jsp">
<jsp:param name="tooltip" value="household.type.tooltip"/>
<jsp:param name="bundle" value="order"/>
</jsp:include>
</div>
<div class="field fieldRadio">
<form:radiobutton path="household.type" value="1"/>
<fmt:message key="household.type1"/>
<form:radiobutton path="household.type" value="2"/>
<fmt:message key="household.type2"/>
<form:errors path="household.type" cssClass="error"/>
</div>
<div class="formclear"></div>
<div class="blockNohousehold">
<!-- household.zones -->
<jsp:include page="/WEB-INF/jspf/edit_elem/radio_yes_no.jsp">
<jsp:param name="label" value="household.zones"/>
<jsp:param name="field" value="household.zones"/>
<jsp:param name="bundle" value="order"/>
<jsp:param name="mandatory" value="true"/>
</jsp:include>
</div>
<div id="blockMonument">
<!-- household.monument -->
<div class="label">
<form:label path="household.monument">
<span class="blockHousehold"><fmt:message key="household.monument1"/></span>
<span class="blockNohousehold"><fmt:message key="household.monument2"/></span>
<span class="formAsterics">*</span>
</form:label>
</div>
<div class="field fieldRadio">
<fmt:bundle basename="bundles.application-resources">
<form:radiobutton path="household.monument" cssClass="inputCheck" value="1"/>
<fmt:message key="yes"/>
<form:radiobutton path="household.monument" cssClass="inputCheck" value="0"/>
<fmt:message key="no"/>
</fmt:bundle>
<form:errors path="household.monument" cssClass="error"/>
</div>
<div class="formclear"></div>
</div>
</fieldset>
</fmt:bundle>
And as last here is my DTO Object Household.java:
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@XmlAccessorType(XmlAccessType.FIELD)
public class Household implements Serializable {
@Range(min = 1, max = 2)
private Integer type;
@Range(min = 0, max = 1)
private Integer zones;
@Range(min = 0, max = 1)
private Integer monument;
}
Im sorry for my english and sorry if there is something missing in my question, just let me know!
Thanks!
My problem is, when i select the options first time, it works pretty good except the class7. When i change the selected options or switch only one then nothing happens anymore. It looks like its frozen. I tried different ways to sort the if´s but this is the best result until now. Im pretty new to jQuery and i would appreciate someones help! 🙂
3
Answers
check the below sample code to show and hide element.
Here is one way of doing it with minimal code. But it requires that the visibility settings are done per element in an extra
data-show
attribute. This attribute contains a string with at most three characters, one for each of the radio buttons forhousehold
,monument
andzones
. The characters are checked one by one in the.every()
loop: ifv=="x"
then thei
-th radio button value must be non-zero, ifv=="-"
then nothing is required and ifv
is anything else then thei
-th radio button’s value must match exactly that.In this kind of code where the logic is convoluted, it is necessary to use logical tools to represent the decision system.
Otherwise you end up with a spaghetti code where you will sink, you and all those who will have to manage this code.
My advice: use "talking" words, not
X
,Y
,Z
names that will make everyone lose the thread of logic.Here, there is only a simple decision grid to maintain, whose logic is modeled on the description of the ins and outs, in a generic way. any change in the logic just requires modifying only this grid, you can even add new classes or new radio buttons, add to them or change their values. the JS could stay the same.
Code point of view, it is of course necessary to bother to establish an interpreter of this grid; for any computer scientist it is a noble part to achieve…
Actualy your decision grid is something like that :
Changed in a JS object it does :
once the code is established, it is much easier to update the decision grid than to revise your nested if cascades
So your code should be :