Start Coding Start Coding - 7 months ago 21
Javascript Question

Multiple selections from dropdown should perform same operation - jQuery

I have a dropdown list which has options- A,B,C,D. I want to hide other input fields based on the selection from dropdown.

I can successfully hide fields if user selects A. I dont know how to hide fields if user selects either A or B.

Working Code:

$('#lockForm select[name="Key"]').change(function () {
if ($('#lockForm select[name="Key"] option:selected').val() == 'A') {
} else {

<form:form method="post" id="lockForm">
<div class="plselect">
<form:select path="Key">
<form:option value="NONE" label="------- Select One -------" />
<form:options items="${List}" itemValue="Code" itemLabel="Desc"/>


You could just make two checks in your if statement with an OR:

if ($('#lockForm select[name="Key"] option:selected').val() == 'A' || '#lockForm select[name="Key"] option:selected').val() == 'B' )

You could clean it up further by saying

var selectedOption = $('#lockForm select[name="Key"] option:selected').val();
if (selectedOption == 'A' || selectedOption == 'B' )