Enabled Disabled Select option based on previous dropdown? - html

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/

Related

How can I get data for select box and set the data while using searching?

<div>
<label for="sort">sort</label>
<select name="sort" id="sort" class="form-control">
<option value=""></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div>
<div>
<label for="label">label</label>
<select name="label" id="label" class="form-control">
<option value=""></option>
<option value="label1">label1</option>
<option value="label2">label2</option>
</select>
</div>
There is a search function, so when I choose the sort and label select box then click the search button, it will sort data. But the selected value is not set in the select box.
How can I solve this? Please help me to solve this.
Add any event to get the selected dropdown value. Below, I have added a select change event.
$("#label").on("change", function(){
//get the data using the above ID's
var cars = ["Saab", "Volvo", "BMW"]; //example array
//appending to the new Select #newData
var htmlContent = "";
htmlContent = "<option value=''></option>";
$.each( cars, function( i, val ) {
$( "#" + val ).text( "Mine is " + val + "." );
htmlContent += "<option value= "+val+">"+val+"</option>";
});
$("#newData").append(htmlContent);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<label for="sort">sort</label>
<select name="sort" id="sort" class="form-control">
<option value=""></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div>
<div>
<label for="label">label</label>
<select name="label" id="label" class="form-control">
<option value=""></option>
<option value="label1">label1</option>
<option value="label2">label2</option>
</select>
</div>
<div>
<label for="newData">New Data</label>
<select name="newData" id="newData" class="form-control">
</select>
</div>

Hide placeholder text from dropdown menu

I am trying to hide the placeholder text in a dropdown menu from showing up as a selectable option. I have searched other options on stack overflow and tried the following solution but none have worked:
<option style="display:none" >Select Stop</option>
<option value="" disabled selected hidden >Select Stop</option>
<option value="" disabled selected style="display: none;">Select Stop</option>
My code looks like this which WORKS in stackoverflow but not when I put it into my site using latest version of chrome...
<select>
<option value="" disabled selected hidden>Select Stop</option>
<option value="home">home</option>
<option value="school">school</option>
<option value="office">office</option>
</select>
Not sure what are you actually trying achieve, to hide the select or option simply add disabled to them, see below, i have 2 select, one with option disbaled other one the entire select disabled
<select>
<option value="">Select Stop</option>
<option value="home" >home</option>
<option value="school" disabled>school</option>
<option value="office" >office</option>
</select>
<select disabled>
<option value="" >Select Stop</option>
<option value="home" >home</option>
<option value="school" >school</option>
<option value="office" >office</option>
</select>
Maybe you might want to add some JQuery into your file
$(function() {
$('.location').find('option:contains(office)').hide();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class='location'>
<option value="select stop">Select Stop</option>
<option value="home">home</option>
<option value="school">school</option>
<option value="office">office</option>
</select>

Two HTML-select boxes linked to each other

Hellloooooo...
I have two <select> inputs like this
<select id="sel1">
<option value="a">a</option>
<option value="b">b</option>
</select>
<select id="sel2">
//if 'a' is selected above,
//<option>apple</option>, <option>airplane</option>
//if 'b' is selected above,
//<option>banana</option>, <option>book</option>
</select>
And I want to list different sets of options according to the selection in sel1.
I could get the selected value using onchange attribute like this:
<select id="sel1" onchange="giveSelection(this)">
<script type="text/javascript">
function giveSelection(sel) {
var selected = sel.value;
}
</script>
But I can't come up with a way to use this value to output different select options in sel2.
Help please!
You almost got there. As you already got sel1's value, the rest is to filter options for sel1 based on the value.
var sel1 = document.querySelector('#sel1');
var sel2 = document.querySelector('#sel2');
var options2 = sel2.querySelectorAll('option');
function giveSelection(selValue) {
sel2.innerHTML = '';
for(var i = 0; i < options2.length; i++) {
if(options2[i].dataset.option === selValue) {
sel2.appendChild(options2[i]);
}
}
}
giveSelection(sel1.value);
<select id="sel1" onchange="giveSelection(this.value)">
<option value="a">a</option>
<option value="b">b</option>
</select>
<select id="sel2">
<option data-option="a">apple</option>
<option data-option="a">airplane</option>
<option data-option="b">banana</option>
<option data-option="b">book</option>
</select>
I drummed up a version that can handle an unlimited amount of select combo dropdown list boxes, using jQuery. Hope this is useful to someone.
function jq_ChainCombo(el) {
var selected = $(el).find(':selected').data('id'); // get parent selected options' data-id attribute
// get next combo (data-nextcombo attribute on parent select)
var next_combo = $(el).data('nextcombo');
// now if this 2nd combo doesn't have the old options list stored in it, make it happen
if(!$(next_combo).data('store'))
$(next_combo).data('store', $(next_combo).find('option')); // store data
// now include data stored in attribute for use...
var options2 = $(next_combo).data('store');
// update combo box with filtered results
$(next_combo).empty().append(
options2.filter(function(){
return $(this).data('option') === selected;
})
);
// now enable in case disabled...
$(next_combo).prop('disabled', false);
// now if this combo box has a child combo box, run this function again (recursive until an end is reached)
if($(next_combo).data('nextcombo') !== undefined )
jq_ChainCombo(next_combo); // now next_combo is the defining combo
}
// quick little jquery plugin to apply jq_ChainCombo to all selects with a data-nextcombo on them
jQuery.fn.chainCombo = function() {
// find all divs with a data-nextcombo attribute
$('[data-nextcombo]').each(function(i, obj) {
$(this).change(function (){
jq_ChainCombo(this);
});
});
}();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!--
// select boxes...
1) first combo simply add data-nextcombo (class, id, etc) and onchange function
2) next combo that is controlled by first, add data-nextcombo (add onchange="jq_ChainCombo(this)" if not using the jquery plugin) and disable to hide entire option list until required.
3) last combo, don't bother adding a data-nextcombo to it
// data...
1) data should have a data-id, and data-option tag
2) data-option should be -1 if it is the root combo box, or if a child combo, the data-option should be the data-id of the parent.
-->
<!-- parent -->
<select class="combo-a" data-nextcombo=".combo-b">
<option value="1" data-id="1" data-option="-1">Bob's Coffee</option>
<option value="2" data-id="2" data-option="-1">Sally's Diner</option>
<option value="3" data-id="3" data-option="-1">Jim's Waffle House</option>
<option value="4" data-id="4" data-option="-1">No Mart</option>
<option value="5" data-id="5" data-option="-1">Ye Olde Ale Tavern</option>
</select>
<!-- child -->
<select class="combo-b" data-nextcombo=".combo-c" disabled>
<option></option>
<option value="1" data-id="1" data-option="1">15th's Avenue Coffee House</option>
<option value="2" data-id="2" data-option="4">14th Street location</option>
<option value="3" data-id="3" data-option="4">13th Avenue</option>
<option value="4" data-id="4" data-option="3">Scarlet Temple Location</option>
<option value="5" data-id="5" data-option="2">New Jack City</option>
<option value="6" data-id="6" data-option="5">Bob street</option>
<option value="7" data-id="7" data-option="1">Billy Avenue</option>
</select>
<!-- child/last -->
<select class="combo-c" disabled>
<option></option>
<option value="4" data-id="4" data-option="2">new display</option>
<option value="3" data-id="3" data-option="3">new display</option>
<option value="5" data-id="5" data-option="2">new display</option>
<option value="6" data-id="6" data-option="2">new display</option>
<option value="7" data-id="7" data-option="4">display #1</option>
<option value="8" data-id="8" data-option="5">new display</option>
<option value="9" data-id="9" data-option="5">new display</option>
<option value="10" data-id="10" data-option="5">new display</option>
<option value="11" data-id="11" data-option="5">new display</option>
<option value="12" data-id="12" data-option="5">new display</option>
<option value="13" data-id="13" data-option="6">new display</option>
<option value="14" data-id="14" data-option="6">new display</option>
<option value="15" data-id="15" data-option="6">new display</option>
<option value="16" data-id="16" data-option="1">Front's Counter</option>
<option value="17" data-id="17" data-option="1">back counter</option>
<option value="18" data-id="18" data-option="4">display #2</option>
</select>

How to show disable HTML select option in by default?

I am new to HTML and PHP and want to achieve a drop-down menu from the mysql table and hard-coded too. I have multiple select in my page, One of them is
<select name="tagging">
<option value="">Choose Tagging</option>
<option value="Option A">Option A</option>
<option value="Option B">Option B</option>
<option value="Option C">Option C</option>
</select>
Problem is now that user can also select "Choose Tagging" as his tagging but i only want to provide him to chose from available three. I used disable as
<select name="tagging">
<option value="" disabled="disabled">Choose Tagging</option>
<option value="Option A">Option A</option>
<option value="Option B">Option B</option>
<option value="Option C">Option C</option>
</select>
But now "Option A" became the default one. So i want to set "Choose Tagging" as by default and also want to disable it from selection. Is it a way to do this. Same thing need to be done with other select which will fetch data from Mysql. Any suggestion will be appreciable.
use
<option selected="true" disabled="disabled">Choose Tagging</option>
Use hidden.
<select>
<option hidden>Choose</option>
<option>Item 1</option>
<option>Item 2</option>
</select>
This doesn't unset it but you can however hide it in the options while it's displayed by default.
In HTML5, to select a disabled option:
<option selected disabled>Choose Tagging</option>
Electron + React.
Let your two first options look like this.
<option hidden="true">Choose Tagging</option>
<option disabled="disabled" default="true">Choose Tagging</option>
First to display when closed.
Second to display first when the list opens.
I know you ask how to disable the option, but I figure the end users visual outcome is the same with this solution, although it is probably marginally less resource demanding.
Use the optgroup tag, like so :
<select name="tagging">
<optgroup label="Choose Tagging">
<option value="Option A">Option A</option>
<option value="Option B">Option B</option>
<option value="Option C">Option C</option>
</optgroup>
</select>
Another SELECT tag solution for those who want to keep first option blank.
<label>Unreal :</label>
<select name="unreal">
<option style="display:none"></option>
<option>Money</option>
<option>Country</option>
<option>God</option>
</select>
<select name="dept" id="dept">
<option value =''disabled selected>Select Department</option>
<option value="Computer">Computer</option>
<option value="electronics">Electronics</option>
<option value="aidt">AIDT</option>
<option value="civil">Civil</option>
</select>
use "SELECTED" which option you want to select by defult.
thanks
selected disabled="true"
Use this. It will work in new browsers
we can disable using this technique.
<select class="form-control" name="option_select">
<option selected="true" disabled="disabled">Select option </option>
<option value="Option A">Option A</option>
<option value="Option B">Option B</option>
<option value="Option C">Option C</option>
</select>
If you are using jQuery to fill your select element, you can use this:
html
<select id="tagging"></select>
js
array_of_options = ['Choose Tagging', 'Option A', 'Option B', 'Option C']
$.each(array_of_options, function(i, item) {
if(i==0) { sel_op = 'selected'; dis_op = 'disabled'; } else { sel_op = ''; dis_op = ''; }
$('<option ' + sel_op + ' ' + dis_op + '/>').val(item).html(item).appendTo('#tagging');
})
This will allow the user to see the first option as a disabled heading ('Choose Tagging'), and select all other options.
this should help....:)
<label for="cars">Choose a car:</label>
<select name="cars" id="cars">
<option value=""selected="true" disabled="disabled"> Select</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
This will hide the option for you this easier that to make a other dropdown box
<option selected="true" disabled="disabled">Choose Tagging</option>
You can set which option is selected by default like this:
<option value="" selected>Choose Tagging</option>
I would suggest using javascript and JQuery to observe for click event and disable the first option after another has been selected:
First, give the element an ID like so:
<select id="option_select" name="tagging">
and the option an id :
<option value="" id="initial">Choose Tagging</option>
then:
<script type="text/javascript">
$('option_select').observe(click, handleClickFunction);
Then you just create the function:
function handleClickFunction () {
if ($('option_select').value !== "option_select")
{
$('initial').disabled=true; }
}

HTML Form first dropdown auto change the second dropdown options

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;
}