Reputation: 33
i got one more doubt im close to the answer but not getting it to worked, Actually i have the a default input text & default drop-down(drop-down which consist of west Bengal & others). NOW if some one click's on the west Bengal state under drop-down then the default input should get hide and the west Bengal drop-down should get displayed.Below is the code what i have tried.can any one please guideme a bit new to jQuery.
thanks!!
<div class="col-sm-4">
<div class="form-group">
<select id="state" name="state" class="form-control" required="true" autocomplete="false" style="margin-bottom:10px">
<option value="" disabled="" selected="">Select State</option>
<option value="WestBengal">West Bengal</option>
<option value="Others">Others</option>
</select>
</div>
</div>
<div class="col-sm-4">
<div class="form-group otherdistricts">
<input class="form-control form-control-lg" type="text" name="other_district" id="other_district" placeholder="Enter Your District" autocomplete="false">
</div>
<div class="westbengaldistrict" style="display:none">
<select class="form-control" name="district" id="district" autocomplete="false">
<option value="" selected disabled>Select Your District</option>
<option value="Alipurduar">Alipurduar</option>
<option value="Bankura">Bankura</option>
<option value="PaschimBardhaman">Paschim Bardhaman</option>
<option value="PurbaBardhaman">Purba Bardhaman</option>
<option value="Birbhum">Birbhum</option>
<option value="CoochBehar">Cooch Behar</option>
<option value="Darjeeling">Darjeeling</option>
<option value="UttarDinajpur">Uttar Dinajpur</option>
<option value="DakshinDinajpur">Dakshin Dinajpur</option>
<option value="Hooghly">Hooghly</option>
<option value="Howrah">Howrah</option>
<option value="Jalpaiguri">Jalpaiguri</option>
<option value="Jhargram">Jhargram</option>
<option value="UttarDinajpur">Uttar Dinajpur</option>
<option value="Kalimpong">Kalimpong</option>
<option value="Malda">Malda</option>
<option value="PaschimMedinipur">Paschim Medinipur</option>
<option value="PurbaMedinipur">Purba Medinipur</option>
<option value="Murshidabad">Murshidabad</option>
<option value="Nadia">Nadia</option>
<option value="North24Parganas">North 24 Parganas</option>
<option value="South24Parganas">South 24 Parganas</option>
<option value="Purulia">Purulia</option>
</select>
</div>
</div>
<script>
$(document).ready(function(){
$("#state").on("select",function() {
if ($(this).val() === "WestBengal") {
$(".otherdistricts").hide();
$(".westbengaldistrict").show();
}
});
});
</script>
Upvotes: 0
Views: 727
Reputation: 51
Below is the updated jquery code as per the updated question. It is working fine and tested.
$(document).ready(function(){
$("#state").change(function() {
if ($(this).val() === "WestBengal") {
$(".otherdistricts").hide();
$(".westbengaldistrict").show();
}else{
$(".otherdistricts").show();
$(".westbengaldistrict").hide();
}
});
});
Upvotes: 0
Reputation: 456
answer to your updated question
$(document).ready(function(){
$(".westbengaldistrict").hide(); // first hide the select
$("#state").on("change",function() {
if ($(this).val() === "WestBengal") {
$(".otherdistricts").hide();
$(".westbengaldistrict").show(); // show on change
}
});
});
Upvotes: 0
Reputation: 317
You have several issues in your code. First of all, your closing brackets are wrong. You should have )}; instead of });
Your inputfield has display: none, you should remove that, because $(".enterotherstates").show(); won't let it appear, only the parent element.
Next, you don't need to iterate with each. It's easier and faster to use $(this).
$(document).ready(function() {
$("select").change(function() {
if ($(this).val() === "WestBengal") {
$(".enterotherstates").hide();
$(".enterbengaldistricts").show();
} else {
$(".enterbengaldistricts").hide();
$(".enterotherstates").show();
}
});
});
Optional performance advice
you could make your code faster by caching your jQuery objects and reuse them. So the browser doesn't have to search the complete HTML again and again:
$(document).ready(function() {
var otherStates = $(".enterotherstates");
var bengal = $(".enterbengaldistricts");
$("select").change(function() {
if ($(this).val() === "WestBengal") {
otherStates.hide();
bengal.show();
} else {
bengal.hide();
otherStates.show();
}
});
});
Upvotes: 0
Reputation: 123
$(document).ready(function () {
$("select").change(function () {
if ($(this).val() === "WestBengal") {
$(".enterotherstates").hide();
$(".enterbengaldistricts").show();
}
else {
$(".enterbengaldistricts").hide();
$(".enterotherstates").show();
}
});
});
Your code has error. Your code is:
$(document).ready(function())};
Change to:
$(document).ready(function()});
Since select
is a form element you can right use val()
method instead of $(this).attr("value")
to get value.
Upvotes: 0
Reputation: 342
Your closing parentheses and brackets are the other way around in your javaScript code.
Should be like this
$(document).ready(function () { code here });
But you have
$(document).ready(function () { code here )};
Try this
<script>
$(document).ready(function () {
$("select").change(function () {
$("select option:selected").each(function () {
//enter bengal districts
if ($(this).attr("value") == "WestBengal") {
$(".enterotherstates").hide();
$(".enterbengaldistricts").show();
}
//enter other states
if ($(this).attr("value") == "Others") {
$(".enterbengaldistricts").hide();
$(".enterotherstates").show();
}
});
});
});
</script>
Upvotes: 0
Reputation: 51
You have errors in the jquery code. Use the below code, it is working fine and tested.
<script>
$(document).ready(function(){
$("select").change(function(){
$( "select option:selected").each(function(){
//enter bengal districts
if($(this).attr("value")=="WestBengal"){
$(".enterotherstates").hide();
$(".enterbengaldistricts").show();
}
//enter other states
if($(this).attr("value")=="Others"){
$(".enterbengaldistricts").hide();
$(".enterotherstates").show();
}
});
});
});
</script>
Upvotes: 1