HTML Form first dropdown auto change the second dropdown options - html

I have this code on the first dropdown:
<td align="left" style="padding-bottom:10px">
<select name="category" id="category">
<option selected value="Please Select">Please Select</option>
<option value="Cars">Cars</option>
<option value="Trucks">Trucks</option>
<option value="Motorcycles">Motorcycles</option>
<option value="Boats">Boats</option>
</select>
</td>
and i also have a second category with different colors for the previous option.
For example:
If i select Cars the second dropdown will appear to select "Red", "Green" or "Blue" but if i select Trucks the second dropdown will appear to select "Black" or "White" options only.
Both second dropdown options will go to the same column (Subcategory) in the mySQL DB so no Car can have the value "Black" on Subcategory column or no Truck can have the value "Red" on Subcategory column.
Thanks

Working example: http://jsfiddle.net/7YeL6/4/
Given this structure:
<select name="category" id="category">
<option selected value="Please Select">Please Select</option>
<option value="Cars">Cars</option>
<option value="Trucks">Trucks</option>
<option value="Motorcycles">Motorcycles</option>
<option value="Boats">Boats</option>
</select>
<div>
<select name="category2" id="truck" class="second">
<option value="white">white</option>
<option value="black">black</option>
</select>
<select name="category2" id="car" class="second">
<option value="red">red</option>
<option value="green">green</option>
<option value="blue">blue</option>
</select>
</div>
You could use jQuery .change function:
$("#category").change(function () {
var str = "";
str = $("select#category option:selected").text();
if(str == "Trucks"){
$("select.second").not("#truck").hide();
$("#truck").show();
$("#truck").fadeIn(1000);
}
else if(str == "Cars"){
$("select.second").not("#car").hide();
$("#car").show();
$("#car").fadeIn(1000);
}
})
CSS
#category2{
display: none;
}

Related

How to make dependable dropdown with jquery

I pre-populated select tag tags with option values coming from PHP variables, and the HTML looks like this:
<!-- Type -->
<select id="d1">
<option type="colours" value="Colours">Colours</option>
<option type="fruit" value="Fruit">Fruit</option>
</select>
<!-- Group -->
<select id="d2">
<option type="colours_dark" subtype="dark" value="Dark">Dark</option>
<option type="colours_light" subtype="light" value="Light">Light</option>
<option type="fruit_ripe" value="Ripe">Ripe</option>
<option type="fruit_rotten" value="Rotten">Rotten</option>
</select>
<!-- Items -->
<select id="d3" style="display: none;">
<option value="">Choose a color</option>
<option type="colours_dark" value="Black">Black</option>
<option type="colours_dark" value="Brown">Brown</option>
<option type="colours_dark" value="Grey">Grey</option>
<option type="colours_light" value="Red">Red</option>
<option type="colours_light" value="Green">Green</option>
<option type="colours_light" value="Blue">Blue</option>
<option type="fruit_ripe" value="Banana">Banana</option>
<option type="fruit_ripe" value="Apple">Apple</option>
<option type="fruit_ripe" value="Orange">Orange</option>
<option type="fruit_rotten" value="Pear">Pear</option>
<option type="fruit_rotten" value="Peach">Peach</option>
<option type="fruit_rotten" value="Mango">Mango</option>
</select>
Initially, I don't want the third dropdown to show because the values will depend on the second dropdown, so I added the following to my jQuery snippet:
var dropDown1Selected = dropDown1.find('option').filter(':selected').val();
if(dropDown3.find('option').filter(':selected').val() == '') {
dropDown3.hide();
}
Based on my HTML, how do I make the options of dropdown 2 show depending on dropdown 1, and then have dropdown 3 showing depending on dropdown 3?
Eg: If I select fruit from dropdown 1, dropdown 2 should only show Ripe, and Rotten. And if I choose Ripe, then dropdown 3 should only show Apple and Orange.
I tried the following in my jQuery snippet, but am struggling to change the dropdown options:
$('#d1').on('change', function(e) {
var val = this.value;
// check if val exists in d2 options
$("#d2 option").filter(function() {
});
}).change();
Hope this work for you, I make dropdown 3 filtered with dropdown 2 so you don't need to hide it
$(document).ready(function () {
filterOption("d1", "d2");
filterOption("d2", "d3");
})
$('#d1').on('change', function (e) {
filterOption("d1", "d2");
filterOption("d2", "d3");
})
$('#d2').on('change', function (e) {
filterOption("d2", "d3");
})
function filterOption(parent, child) {
var parent = $(`#${parent} :selected`).attr('type');
$(`#${child} option`).each(function () {
if ($(this).val() == "" || $(this).attr('type').includes(parent)) {
$(this).show();
}
else {
$(this).hide();
}
})
$(`#${child}`).val($(`#${child} option:visible:first`).val());
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Type -->
<select id="d1">
<option type="colours" value="Colours">Colours</option>
<option type="fruit" value="Fruit">Fruit</option>
</select>
<!-- Group -->
<select id="d2">
<option type="colours_dark" subtype="dark" value="Dark">Dark</option>
<option type="colours_light" subtype="light" value="Light">Light</option>
<option type="fruit_ripe" value="Ripe">Ripe</option>
<option type="fruit_rotten" value="Rotten">Rotten</option>
</select>
<!-- Items -->
<select id="d3">
<option value="">Choose a color</option>
<option type="colours_dark" value="Black">Black</option>
<option type="colours_dark" value="Brown">Brown</option>
<option type="colours_dark" value="Grey">Grey</option>
<option type="colours_light" value="Red">Red</option>
<option type="colours_light" value="Green">Green</option>
<option type="colours_light" value="Blue">Blue</option>
<option type="fruit_ripe" value="Banana">Banana</option>
<option type="fruit_ripe" value="Apple">Apple</option>
<option type="fruit_ripe" value="Orange">Orange</option>
<option type="fruit_rotten" value="Pear">Pear</option>
<option type="fruit_rotten" value="Peach">Peach</option>
<option type="fruit_rotten" value="Mango">Mango</option>
</select>

Enabled Disabled Select option based on previous dropdown?

i have 2 dropdown, lets say select 1 and select 2, the scenario is
when user select dropdown on select 1 and choose A, so on select 2, option A will be disabled.
Here is my code, actually this is working,
but i have a condition when user will edit the dropdown, on select 1 user already choose A but select 2 still not selected, what i want is the option A in select 2 must be disabled, but currently it still enable
jsfiddle https://jsfiddle.net/q3mu7x2w/
$("#pr_bundling").change(function(){
$("#pr_cross").find("option").each(function(){
$(this).removeAttr("disabled");
});
$("#pr_cross [value=" + $(this).val() + "]").attr("disabled","disabled");
$('#pr_cross').not(this).has('option[value="'+ this.value + '"]:selected').val('noselect29');
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h3>select 1</h3>
<select class="form-control" id="pr_bundling">
<option value='noselect99' selected >Select</option>
<option value="A" selected>A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
<br>
<h3>select 2</h3>
<select class="form-control" id="pr_cross">
<option value='noselect29' selected >Select</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
</select>
You can ensure that the initial selection from #pr_bundling is disabled on page load by moving your update code into a function and calling that on $(document).ready (either directly or via .trigger).
function update_select() {
let selected = $('#pr_bundling').val();
$("#pr_cross").find("option").each(function() {
$(this).removeAttr("disabled");
});
$("#pr_cross [value=" + selected + "]").attr("disabled", "disabled");
}
$(document).ready(function() {
$("#pr_bundling").change(update_select);
update_select();
// or you can trigger the change handler:
// $("#pr_bundling").trigger('change');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h3>select 1</h3>
<select class="form-control" id="pr_bundling">
<option value='noselect99' selected>Select</option>
<option value="A" selected>A</option>
<option value="B">B</option>
<option value="C">C</option>
</select>
<br>
<h3>select 2</h3>
<select class="form-control" id="pr_cross">
<option value='noselect29' selected>Select</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
</select>
The selector is wrong while disabling the value of select 2.
$("#pr_cross option[value=" + $(this).val() + "]").attr("disabled","disabled");
jsfiddle: https://jsfiddle.net/bs7h0got/

Selecting an option in drop down menu one determines list available in the second drop down menu

I want to make the contains of a second drop down menu dependent on what is selected in the the first. In my case wish to do it for a car selection option. i.e when the car make is selected this then populates the model drop down menu with the relevant models.
As there are a lot of makes of car I would like to keep it concise if possible I presume arrays of models for each make is the solution but how do implement this.
I have look at various solutions and cannot find a suitable answer to this, any suggestions/examples would be gratefully appreciated.
I am using jsp.
Make:<select>
<option value="audi">Audi</option>
<option value="bmw">BMW</option>
<option value="ford">Ford</option>
<option value="toyota">Toyota</option>
</select>
You could do it with some simple JS and css ( here is a JSFiddle: http://jsfiddle.net/N7Q57/1/)
Make:
<select name="cars" id="cars">
<option selected value="Choose">Choose</option>
<option value="Audi">Audi</option>
<option value="BMW">BMW</option>
<option value="Ford">Ford</option>
<option value="Toyota">Toyota</option>
</select>
<div id="brands">
<select class="audi">
<option value="1">1</option>
<option value="2">2</option>
</select>
<select class="bmw">
<option value="3">3</option>
<option value="4">4</option>
</select>
</div
the jQuery
$("#cars").change(function () {
var string = "";
string = $("select#cars option:selected").text();
if (string == "Audi"){
$("#brands .bmw").hide();
$("#brands .audi").show();
$("#brands").show();
} else if (string == "BMW"){
$("#brands .audi").hide();
$("#brands .bmw").show();
$("#brands").show();
}
})
and the CSS:
#brands{
display: none;
}

How to add a title to a html select tag

How would I go about setting a title in select tag? Here is my select box:
<select>
<option value="sydney">Sydney</option>
<option value="melbourne">Melbourne</option>
<option value="cromwell">Cromwell</option>
<option value="queenstown">Queenstown</option>
</select>
When I visit the site, by default it shows "Sydney". But I want to display a title, such as, "What is the name of your city?"
<select>
<option selected disabled>Choose one</option>
<option value="sydney">Sydney</option>
<option value="melbourne">Melbourne</option>
<option value="cromwell">Cromwell</option>
<option value="queenstown">Queenstown</option>
</select>
Using selected and disabled will make "Choose one" be the default selected value, but also make it impossible for the user to actually select the item, like so:
<select>
<optgroup label = "Choose One">
<option value ="sydney">Sydney</option>
<option value ="melbourne">Melbourne</option>
<option value ="cromwell">Cromwell</option>
<option value ="queenstown">Queenstown</option>
</optgroup>
</select>
You can combine it with selected and hidden
<select class="dropdown" style="width: 150px; height: 26px">
<option selected hidden>What is your name?</option>
<option value="michel">Michel</option>
<option value="thiago">Thiago</option>
<option value="Jonson">Jonson</option>
</select>
Your dropdown title will be selected and cannot chose by the user.
You can use the following
<select data-hai="whatup">
<option label="Select your city">Select your city</option>
<option value="sydney">Sydney</option>
<option value="melbourne">Melbourne</option>
<option value="cromwell">Cromwell</option>
<option value="queenstown">Queenstown</option>
</select>
I think that this would help:
<select name="select_1">
<optgroup label="First optgroup category">
<option selected="selected" value="0">Select element</option>
<option value="2">Option 1</option>
<option value="3">Option 2</option>
<option value="4">Option 3</option>
</optgroup>
<optgroup label="Second optgroup category">
<option value="5">Option 4</option>
<option value="6">Option 5</option>
<option value="7">Option 6</option>
</optgroup>
</select>
<option value="" selected style="display:none">Please select one item</option>
Using selected and using display: none; for hidden item in list.
You can add an option tag on top of the others with no value and a prompt like this:
<select>
<option value="">Choose One</option>
<option value ="sydney">Sydney</option>
<option value ="melbourne">Melbourne</option>
<option value ="cromwell">Cromwell</option>
<option value ="queenstown">Queenstown</option>
</select>
Or leave it blank instead of saying Choose one if you want.
Typically, I would suggest that you use the <optgroup> option, as that gives some nice styling and indenting to the element.
The HTML element creates a grouping of options within a element. (Source: MDN Web Docs: <optgroup>.
But, since an <optgroup> cannot be a selected value, you can make an <option selected disabled> and then stylize it with CSS so that it behaves like an <optgroup>....
.optionGroup {
font-weight: bold;
font-style: italic;
}
<select>
<option class="optionGroup" selected disabled>Choose one</option>
<option value="sydney" class="optionChild"> Sydney</option>
<option value="melbourne" class="optionChild"> Melbourne</option>
<option value="cromwell" class="optionChild"> Cromwell</option>
<option value="queenstown" class="optionChild"> Queenstown</option>
</select>
With a default option having selected attribute
<select>
<option value="" selected>Choose your city</option>
<option value ="sydney">Sydney</option>
<option value ="melbourne">Melbourne</option>
<option value ="cromwell">Cromwell</option>
<option value ="queenstown">Queenstown</option>
</select>
The first option's text will always display as default title.
<select>
<option value ="">What is the name of your city?</option>
<option value ="sydney">Sydney</option>
<option value ="melbourne">Melbourne</option>
<option value ="cromwell">Cromwell</option>
<option value ="queenstown">Queenstown</option>
</select>
You can create dropdown title | label with selected, hidden and style for old or unsupported device.
<select name="city" >
<option selected hidden style="display:none">What is your city</option>
<option value="1">Sydney</option>
<option value="2">Melbourne</option>
<option value="3">Cromwell</option>
<option value="4">Queenstown</option>
</select>
I added a <div> and it worked fine
<div title="Your title here!">
<select>
<option value="sydney">Sydney</option>
<option value="melbourne">Melbourne</option>
<option value="cromwell">Cromwell</option>
<option value="queenstown">Queenstown</option>
</select>
</div>
<select name="city">
<option value ="0">What is your city</option>
<option value ="1">Sydney</option>
<option value ="2">Melbourne</option>
<option value ="3">Cromwell</option>
<option value ="4">Queenstown</option>
</select>
You can use the unique id for the value instead

html onClick condition Link

SO basically im trying to create a button on a html webpage that uses or checks the values from two drop down lists and relative to that it will link to the appropriate page.
But the IF statements dont seem to work...
i tried using regular href Links but this didnt end well, such as When to use onclick in HTML?
here is my code:
<select name="Module">
<option value="Module 10">Module 10</option>
<option value="Module 11">Module 11</option>
<option value="Module 20">Module 20</option>
<option value="Module 60">Module 60</option>
<option value="Module 70">Module 70</option>
<option value="Module 85">Module 85</option>
</select>
<select name="Sector">
<option value="Sector1">Sector1</option>
<option value="Sector2">Sector2</option>
<option value="Sector3">Sector3</option>
<option value="Sector4">Sector4</option>
</select>
<FORM>
<INPUT type="button" value="OK"
onClick="if (( Module=='Module 10') && (Sector=='Sector1)) {location.href='M10_S1.html'}">
</FORM>
This should be what you want:
<form>
<select name="Module">
<option value="1">Module 1</option>
<option value="2">Module 2</option>
<option value="3">Module 3</option>
<option value="4">Module 4</option>
<option value="5">Module 5</option>
<option value="6">Module 6</option>
</select>
<select name="Sector">
<option value="1">Sector1</option>
<option value="2">Sector2</option>
<option value="3">Sector3</option>
<option value="4">Sector4</option>
</select>
<input type="button" value="Confirm" onClick="location.href='./M' + Module.value + '_S' + Sector.value + '.htm'">
</form>
The file names will be formatted as "M#_S#.htm"