Right now I have two dropdowns of numbers. The functionality I want is: when I choose an option out of the first list, compare that selected option's value with the second list's option values and hide any options in the second list that are greater than the first list's selected option value.
<select name="input_6" id="input_2_6" class="medium gfield_select" tabindex="13">
<option value="117500">115,001 - 120,000</option>
<option value="122500">120,001 - 125,000</option>
<option value="127500">125,001 - 130,000</option>
<option value="132500">130,001 - 135,000</option>
<option value="137500">135,001 - 140,000</option>
<option value="142500">140,001 - 145,000</option>
<option value="147500">145,001 - 150,000</option>
<option value="152500">150,001 - 155,000</option>
<option value="157500">155,001 - 160,000</option>
<option value="162500">160,001 - 165,000</option>
<option value="167500">165,001 - 170,000</option>
<option value="172500">170,001 - 175,000</option>
<option value="177500">175,001 - 180,000</option>
<option value="182500">180,001 - 185,000</option>
<option value="187500">185,001 - 190,000</option>
<option value="192500">190,001 - 195,000</option>
<option value="197500">195,001 - 200,000</option>
<option value="205000">200,001 - 210,000</option>
<option value="215000">210,001 - 220,000</option>
<option value="225000">220,001 - 230,000</option>
<option value="235000">230,001 - 240,000</option>
<option value="245000">240,001 - 250,000</option>
<option value="255000" selected="selected">250,001 - 260,000</option>
<option value="265000">260,001 - 270,000</option>
<option value="275000">270,001 - 280,000</option>
<option value="285000">280,001 - 290,000</option>
<option value="295000">290,001 - 300,000</option>
<option value="305000">300,001 - 310,000</option>
<option value="315000">310,001 - 320,000</option>
<option value="325000">320,001 - 330,000</option>
<option value="335000">330,001 - 340,000</option>
<option value="345000">340,001 - 350,000</option>
<option value="355000">350,001 - 360,000</option>
</select>
<select name="input_7" id="input_2_7" class="medium gfield_select" tabindex="16">
<option value="102500" selected="selected">100,000 - 105,000</option>
<option value="107500">105,000 - 110,000</option>
<option value="112500">110,000 - 115,000</option>
<option value="117500">115,000 - 120,000</option>
<option value="122500">120,000 - 125,000</option>
<option value="127500">125,000 - 130,000</option>
<option value="132500">130,000 - 135,000</option>
<option value="137500">135,000 - 140,000</option>
<option value="142500">140,000 - 145,000</option>
<option value="147500">145,000 - 150,000</option>
<option value="152500">150,000 - 155,000</option>
<option value="157500">155,000 - 160,000</option>
<option value="162500">160,000 - 165,000</option>
<option value="167500">165,000 - 170,000</option>
<option value="172500">170,000 - 175,000</option>
<option value="177500">175,000 - 180,000</option>
<option value="182500">180,000 - 185,000</option>
<option value="187500">185,000 - 190,000</option>
<option value="192500">190,000 - 195,000</option>
<option value="197500">195,000 - 200,000</option>
<option value="202500">200,000 - 205,000</option>
<option value="207500">205,000 - 210,000</option>
<option value="212500">210,000 - 215,000</option>
<option value="217500">215,000 - 220,000</option>
<option value="222500">220,000 - 225,000</option>
<option value="227500">225,000 - 230,000</option>
<option value="232500">230,000 - 235,000</option>
<option value="237500">235,000 - 240,000</option>
<option value="242500">240,000 - 245,000</option>
<option value="247500">245,000 - 250,000</option>
<option value="252500">250,000 - 255,000</option>
<option value="257500">255,000 - 260,000</option>
<option value="262500">260,000 - 265,000</option>
<option value="267500">265,000 - 270,000</option>
<option value="272500">270,000 - 275,000</option>
<option value="277500">275,000 - 280,000</option>
<option value="282500">280,000 - 285,000</option>
<option value="287500">285,000 - 290,000</option>
<option value="292500">290,000 - 295,000</option>
<option value="297500">295,000 - 300,000</option>
<option value="302500">300,000 - 305,000</option>
<option value="307500">305,000 - 310,000</option>
<option value="312500">310,000 - 315,000</option>
<option value="317500">315,000 - 320,000</option>
<option value="322500">320,000 - 325,000</option>
<option value="327500">325,000 - 330,000</option>
<option value="332500">330,000 - 335,000</option>
<option value="337500">335,000 - 340,000</option>
<option value="342500">340,000 - 345,000</option>
<option value="347500">345,000 - 350,000</option>
<option value="352500">350,000 - 355,000</option>
</select>
Above are the two lists I'm working with (generated by gravity forms).
And here are two variations of jQuery I've tried so far:
<script type="text/javascript">
$('#input_2_6').change(function(){
var initial = $('#input_2_6 option:selected').val();
secondDropPop(initial);
function secondDropPop(i){
$('#input_2_7 option').each(function(){
if ($(this).val() > i){
$(this).css('display','none');
}
});
}
});
</script>
And
<script type="text/javascript">
$('#input_2_6').change(function(){
$('#input_2_6 option').each(function(){
if($(this).is(':selected')){
var initial = $(this).val();
secondDropPop(initial);
}
});
function secondDropPop(i){
$('#input_2_7 option').each(function(){
if ($(this).val() > i){
$(this).css('display','none');
}
});
}
});
</script>
What am I doing wrong?
jQuery ver: 1.11.3
Per jQuery documention
In the case of an array, the callback is passed an array index and a corresponding array value each time.
Looking at$(this).val() > i, i is not defined. This should be the first argument of your callback:
$('#input_2_7 option').each(function(i){
i = parseInt(i, 10);
if (parseInt($(this).val(), 10) > i){
$(this).css('display','none');
}
});
Edit
The problem here is that you are trying to apply styles to option tags which is a very unstable approach as each browser applies styles differently to option tags (some browsers do not allow any styles to apply). So your best bet is to create the second dropdown on the fly. Below is an example of this approach.
$('#input_2_6').change(function(){
var initial = $('#input_2_6 option:selected').val();
secondDropPop(initial);
function secondDropPop(i){
$('#input_2_7 option').each(function(){
if ($(this).val() > i){
$(this).css('display','none');
}
});
}
});
$('#input_2_6').change(function(){
$('#input_2_6 option').each(function(){
if($(this).is(':selected')){
var initial = $(this).val();
secondDropPop(initial);
}
});
function secondDropPop(i){
var $dropdown = $('#input_2_7').hide();//hide the dropdown as this will prevent the browser from rendering every time an option is appended
$dropdown.empty();
$('#full_dropdown option').each(function(){
if ($(this).val() <= i){
$dropdown.append($(this).clone());
}
});
$dropdown.show();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="input_6" id="input_2_6" class="medium gfield_select" tabindex="13">
<option value="117500">115,001 - 120,000</option>
<option value="122500">120,001 - 125,000</option>
<option value="127500">125,001 - 130,000</option>
<option value="132500">130,001 - 135,000</option>
<option value="137500">135,001 - 140,000</option>
<option value="142500">140,001 - 145,000</option>
<option value="147500">145,001 - 150,000</option>
<option value="152500">150,001 - 155,000</option>
<option value="157500">155,001 - 160,000</option>
<option value="162500">160,001 - 165,000</option>
<option value="167500">165,001 - 170,000</option>
<option value="172500">170,001 - 175,000</option>
<option value="177500">175,001 - 180,000</option>
<option value="182500">180,001 - 185,000</option>
<option value="187500">185,001 - 190,000</option>
<option value="192500">190,001 - 195,000</option>
<option value="197500">195,001 - 200,000</option>
<option value="205000">200,001 - 210,000</option>
<option value="215000">210,001 - 220,000</option>
<option value="225000">220,001 - 230,000</option>
<option value="235000">230,001 - 240,000</option>
<option value="245000">240,001 - 250,000</option>
<option value="255000" selected="selected">250,001 - 260,000</option>
<option value="265000">260,001 - 270,000</option>
<option value="275000">270,001 - 280,000</option>
<option value="285000">280,001 - 290,000</option>
<option value="295000">290,001 - 300,000</option>
<option value="305000">300,001 - 310,000</option>
<option value="315000">310,001 - 320,000</option>
<option value="325000">320,001 - 330,000</option>
<option value="335000">330,001 - 340,000</option>
<option value="345000">340,001 - 350,000</option>
<option value="355000">350,001 - 360,000</option>
</select>
<select name="input_7" id="input_2_7" class="medium gfield_select" tabindex="16" style="display:none">
</select>
<!--Create a new dropdown that is not used for anything except pulling data from. Turning this into an array could speed things up-->
<select id="full_dropdown" style="display:none;" tabindex="16">
<option value="102500" selected="selected">100,000 - 105,000</option>
<option value="107500">105,000 - 110,000</option>
<option value="112500">110,000 - 115,000</option>
<option value="117500">115,000 - 120,000</option>
<option value="122500">120,000 - 125,000</option>
<option value="127500">125,000 - 130,000</option>
<option value="132500">130,000 - 135,000</option>
<option value="137500">135,000 - 140,000</option>
<option value="142500">140,000 - 145,000</option>
<option value="147500">145,000 - 150,000</option>
<option value="152500">150,000 - 155,000</option>
<option value="157500">155,000 - 160,000</option>
<option value="162500">160,000 - 165,000</option>
<option value="167500">165,000 - 170,000</option>
<option value="172500">170,000 - 175,000</option>
<option value="177500">175,000 - 180,000</option>
<option value="182500">180,000 - 185,000</option>
<option value="187500">185,000 - 190,000</option>
<option value="192500">190,000 - 195,000</option>
<option value="197500">195,000 - 200,000</option>
<option value="202500">200,000 - 205,000</option>
<option value="207500">205,000 - 210,000</option>
<option value="212500">210,000 - 215,000</option>
<option value="217500">215,000 - 220,000</option>
<option value="222500">220,000 - 225,000</option>
<option value="227500">225,000 - 230,000</option>
<option value="232500">230,000 - 235,000</option>
<option value="237500">235,000 - 240,000</option>
<option value="242500">240,000 - 245,000</option>
<option value="247500">245,000 - 250,000</option>
<option value="252500">250,000 - 255,000</option>
<option value="257500">255,000 - 260,000</option>
<option value="262500">260,000 - 265,000</option>
<option value="267500">265,000 - 270,000</option>
<option value="272500">270,000 - 275,000</option>
<option value="277500">275,000 - 280,000</option>
<option value="282500">280,000 - 285,000</option>
<option value="287500">285,000 - 290,000</option>
<option value="292500">290,000 - 295,000</option>
<option value="297500">295,000 - 300,000</option>
<option value="302500">300,000 - 305,000</option>
<option value="307500">305,000 - 310,000</option>
<option value="312500">310,000 - 315,000</option>
<option value="317500">315,000 - 320,000</option>
<option value="322500">320,000 - 325,000</option>
<option value="327500">325,000 - 330,000</option>
<option value="332500">330,000 - 335,000</option>
<option value="337500">335,000 - 340,000</option>
<option value="342500">340,000 - 345,000</option>
<option value="347500">345,000 - 350,000</option>
<option value="352500">350,000 - 355,000</option>
</select>
I have a form with 2 radio buttons and when either button is toggled it shows or hides a certain fieldset. The issue I have is because the fieldset is just hidden so when the form is submitted it still takes the first fieldsets values.
I have setup a fiddle to show how the form changes the fieldsets http://jsfiddle.net/hhdMq/1/
So when I select "Scale B" although you can change the correct values, when the form is submitted it takes the default values of Scale A.
<center>
<input type="radio" name="sellorlet" value="Yes" id="rdYes" checked="yes" />
<label for="rdYes">Scale A</label>
<input type="radio" name="sellorlet" value="No" id="rdNo" />
<label for="rdNo">Scale B</label>
</center>
<fieldset id="sell">
<center>
<select id="pricemin" name="min">
<option value="50000">Min Price</option>
<option value="50000">£50,000</option>
<option value="100000">£100,000</option>
<option value="200000">£200,000</option>
<option value="300000">£300,000</option>
<option value="400000">£400,000</option>
<option value="500000">£500,000</option>
<option value="600000">£600,000</option>
<option value="700000">£700,000</option>
<option value="800000">£800,000</option>
<option value="900000">£900,000</option>
<option value="1000000">£1,000,000</option>
<option value="1250000">£1,250,000</option>
<option value="1500000">£1,500,000</option>
<option value="1750000">£1,750,000</option>
<option value="2000000">£2,000,000</option>
<option value="3000000">£3,000,000</option>
</select>
<select id="pricemax" name="max">
<option value="5000000">Max Price</option>
<option value="100000">£100,000</option>
<option value="200000">£200,000</option>
<option value="300000">£300,000</option>
<option value="400000">£400,000</option>
<option value="500000">£500,000</option>
<option value="600000">£600,000</option>
<option value="700000">£700,000</option>
<option value="800000">£800,000</option>
<option value="900000">£900,000</option>
<option value="1000000">£1,000,000</option>
<option value="1250000">£1,250,000</option>
<option value="1500000">£1,500,000</option>
<option value="1750000">£1,750,000</option>
<option value="2000000">£2,000,000</option>
<option value="3000000">£3,000,000</option>
<option value="4000000">£4,000,000</option>
<option value="5000000">£5,000,000</option>
</select>
</center>
</fieldset>
<fieldset id="buy" style="display:none;">
<center>
<select id="lpricemin" name="min">
<option value="500">Min Price</option>
<option value="500">£500</option>
<option value="600">£600</option>
<option value="700">£700</option>
<option value="800">£800</option>
<option value="900">£900</option>
<option value="1000">£1000</option>
<option value="1150">£1150</option>
<option value="1250">£1250</option>
<option value="1500">£1500</option>
<option value="2000">£2000</option>
<option value="2500">£2500</option>
<option value="3000">£3000</option>
<option value="4000">£4000</option>
<option value="5000">£5000</option>
</select>
<select id="lpricemax" name="max">
<option value="5000">Max Price</option>
<option value="600">£600</option>
<option value="700">£700</option>
<option value="800">£800</option>
<option value="900">£900</option>
<option value="1000">£1000</option>
<option value="1150">£1150</option>
<option value="1250">£1250</option>
<option value="1500">£1500</option>
<option value="2000">£2000</option>
<option value="2500">£2500</option>
<option value="3000">£3000</option>
<option value="4000">£4000</option>
<option value="5000">£5000</option>
</select>
</center>
</fieldset>
and the jquery used:
$("input[name='sellorlet']").change(function () {
$("#sell").toggle(this.value == "Yes");
$("#let").toggle(this.value == "No");
});
My question is, how can I completely disable the first fieldset if Scale B is selected and likewise when Scale A is selected it will disable the second fieldset?
Many thanks
When submitting a form, you cannot have two inputs, selects, or textareas with the same name in the same form. Doing so will cause confusion and probably end up the wrong info being submitted. There are two ways you can fix this.
Method 1:
Change the
<select id="lpricemin" name="min">
<select id="lpricemin" name="lmin">
to
<select id="lpricemax" name="max">
<select id="lpricemax" name="lmax"> respectively.
This will allow you to handle the data from lmin and lmax and ensure you get the info from the second fieldset.
Method 2:
Put the second fieldset in a different form. Then just change the jQuery to show the forms instead of the fieldsets.
Changed the radio buttons now to a tabbed design so the form can be separated correctly. If anyone would like to know how the tabs are done they are here http://cssdeck.com/labs/fancy-tabbed-navigation-with-css3-and-jquery
Method 3 would be to change the switch the content of your select through Javascript rather than toggling through visibility. You can do that through:
function setMaxScale()
{
document.getElementById().innerHTML = "<option value="50000">Min Price</option>\
<option value="50000">£50,000</option>\
<option value="100000">£100,000</option>\
<option value="200000">£200,000</option>\
<option value="300000">£300,000</option>...";
}
An other clean solution would be to create your scales dynamically with a loop.