I am trying to show and hide content based on value in select field.
I want to only display content in Div 1 if the value for a select field is 'United States'. And only display content in Div 2 if the value is any other country EXCEPT 'United States. But the 2nd part is not working.
Here is what I have so far. This will display Div 1 if United States selected, but not Div 2 if I select another country.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("select").change(function(){
$( "select option:selected").each(function(){
if($(this).attr("value")=='United States'){
$(".rest1").show();
$(".rest2").hide();
}
if($(this).attr("value")!='United States'){
$(".rest2").show();
$(".rest1").hide();
}
});
}).change();
});
</script>
The following works if I specify another country, but I need Div 2 to display for ALL countries except 'United States'.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("select").change(function(){
$( "select option:selected").each(function(){
if($(this).attr("value")=='United States'){
$(".rest1").show();
$(".rest2").hide();
}
if($(this).attr("value")=='Austria'){
$(".rest2").show();
$(".rest1").hide();
}
});
}).change();
});
</script>
What am I missing?
David Thomas - Here is more of the html. I hope this is enough:
$(document).ready(function() {
$("select").change(function() {
$("select option:selected").each(function() {
if ($(this).attr("value") == 'United States') {
$(".rest1").show();
$(".rest2").hide();
}
if ($(this).attr("value") != 'United States') {
$(".rest2").show();
$(".rest1").hide();
}
});
}).change();
});
.rest1,
.rest2 {
display: none;
}
<div>
<label for="country" class="form-label">Country <span class="text-danger">*</span></label>
<select name="country" id="country" class="form-control" {if option:checked}checked{/if}>
<option value="">Select Country</option>
<option value="Austria" {if country=="Austria" }selected="selected" {/if}>Austria</option>
<option value="Bahamas" {if country=="Bahamas" }selected="selected" {/if}>Bahamas</option>
<option value="Belgium" {if country=="Belgium" }selected="selected" {/if}>Belgium</option>
<option value="Canada" {if country=="Canada" }selected="selected" {/if}>Canada</option>
.....
<option value="United States" {if country=="United States" }selected="selected" {/if}>United States</option>
</select>
</div>
<div class="rest1">
Div 1 content
</div>
<div class="rest2">
Div 2 content
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
You don't need to loop over the options. Just get the value of the select and compare it.
$(document).ready(function() {
$("select").change(function() {
if ($(this).val() == 'United States') {
$(".rest1").show();
$(".rest2").hide();
} else {
$(".rest2").show();
$(".rest1").hide();
}
}).change();
});
.rest1,
.rest2 {
display: none;
}
<div>
<label for="country" class="form-label">Country <span class="text-danger">*</span></label>
<select name="country" id="country" class="form-control">
<option value="">Select Country</option>
<option value="Austria">Austria</option>
<option value="Bahamas">Bahamas</option>
<option value="Belgium">Belgium</option>
<option value="Canada">Canada</option>
<option value="United States">United States</option>
</select>
</div>
<div class="rest1">
Div 1 content
</div>
<div class="rest2">
Div 2 content
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>