How to make the dropdown option disable in HTML web page - javascript

I have web page with three dropdown. It is working fine, except for one problem.
When we select any item from one dropdown then it is disabled in the other two dropdowns, but if we again select another item from first dropdown then it disables this item but it also disables the previously selected item.
The previously selected should be enabled when new option is disabled.
Here is the code:
<select id="select1" name="indication_subject[]">
<option value="" selected="selected">a </option>
<option value="1"> Accounting</option>
<option value="2"> Afrikaans</option>
<option value="3"> Applied Information and Communication Technology</option>
<option value="4"> Arabic</option>
<option value="5"> Art and Design</option>
<option value="6"> Biology</option>
<option value="7"> Business Studies</option>
</select>
<select id="select2" name="indication_subject[]">
<option value="" selected="selected">a </option>
<option value="1"> Accounting</option>
<option value="2"> Afrikaans</option>
<option value="3"> Applied Information and Communication Technology</option>
<option value="4"> Arabic</option>
<option value="5"> Art and Design</option>
<option value="6"> Biology</option>
<option value="7"> Business Studies</option>
</select>
<select id="select3" name="indication_subject[]">
<option value="" selected="selected">a </option>
<option value="1"> Accounting</option>
<option value="2"> Afrikaans</option>
<option value="3"> Applied Information and Communication Technology</option>
<option value="4"> Arabic</option>
<option value="5"> Art and Design</option>
<option value="6"> Biology</option>
<option value="7"> Business Studies</option>
</select>
Here is the JavaScript code:
$(document).ready(function(){
$("select").change(function() {
$("select").not(this).find("option[value="+ $(this).val() + "]").attr('disabled', true);
});
});
Demo Link: http://jsfiddle.net/x4E5Q/137/

jsFiddle
Combine the focus event with the change event to achieve what you want:
JavaScript
$(document).ready(function () {
var previous;
$("select").focus(function () {
// Store the current value on focus and on change
previous = this.value;
}).change(function () {
// Do something with the previous value after the change
$("select").not(this).find("option[value=" + previous + "]").prop('disabled', false);
$("select").not(this).find("option[value=" + $(this).val() + "]").prop('disabled', true);
// Make sure the previous value is updated
previous = this.value;
});
});

Related

Filter 3 Dropdowns menus and get selected options in 3 other filtered dropdonws

First to filter the first 3 dropdowns menus, I used the code found on this page
I can also filter the next 3 as well, but I don't know how to get the selected data from the first 3 menus (Year 1) and show in the next 3 menus (Year 2) only the 3 selected options in Year 1. All the options are the same in all dropdown menus.
Example: Year 1 dropdown 1 : Red; dropdown 2 : Blue and Dropdown 3 : Yellow.
How can i find in Year 2 Dropdown 1 only the 3 options selected previously (Red, Blue, Yellow) and hide the others, idem for Year 2 Dropdown 2 and Year 2 Dropdown 3 (options must also be filtered to avoid multiple selection for Year 2)
Fiddle
$(document).ready(function() {
$("select").on('focus', function() {
$("select").find("option[value='" + $(this).val() + "']").attr('disabled', false);
}).change(function() {
$("select").not(this).find("option[value='" + $(this).val() + "']").attr('disabled', true);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<p>
YEAR 1
<select id="y1sp1" name="indication_subject[]">
<option></option>
<option value="1"> Red</option>
<option value="2"> Blue</option>
<option value="3"> Yellow</option>
<option value="4"> Black</option>
</select>
<select id="y1sp2" name="indication_subject[]">
<option></option>
<option value="1"> Red</option>
<option value="2"> Blue</option>
<option value="3"> Yellow</option>
<option value="4"> Black</option>
</select>
<select id="y1sp3" name="indication_subject[]">
<option></option>
<option value="1"> Red</option>
<option value="2"> Blue</option>
<option value="3"> Yellow</option>
<option value="4"> Black</option>
</select>
</p><p>
YEAR 2
<select id="y2sp1" name="indication_subject[]">
<option></option>
<option value="1"> Red</option>
<option value="2"> Blue</option>
<option value="3"> Yellow</option>
<option value="4"> Black</option>
</select>
<select id="y2sp2" name="indication_subject[]">
<option></option>
<option value="1"> Red</option>
<option value="2"> Blue</option>
<option value="3"> Yellow</option>
<option value="4"> Black</option>
</select>
<select id="y2sp3" name="indication_subject[]">
<option></option>
<option value="1"> Red</option>
<option value="2"> Blue</option>
<option value="3"> Yellow</option>
<option value="4"> Black</option>
</select>
</p>

HTML form with checkbox options to select fieldset

I have a form that is like the following:
<label>
scale a<input type="radio" name="sellorbuy" value="Yes" id="rdYes" />
</label>
<label class="leftspace">
scale b<input type="radio" name="sellorbuy" value="No" id="rdNo" />
</label>
<p class="searchpg">Price</p>
<fieldset id="sell">
<select id="pricemin" name="minsell" class="form-control">
<option value="50000">Min 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>
</select>
<select id="pricemax" name="maxsell" class="form-control">
<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>
</select>
</fieldset>
<fieldset id="let" style="display:none;">
<select id="lpricemin" name="minbuy" class="form-control">
<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>
</select>
<select id="lpricemax" name="maxbuy" class="form-control">
<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>
</select>
</fieldset>
This switches fieldsets by using the following:
$("input[name='sellorlet']").change(function () {
$("#sell").toggle(this.value == "Yes");
$("#let").toggle(this.value == "No");
});
The trouble I am having is the search form that is submitted and displayed on the next page so we carry over the contents of the form. If someone has selected the 'Scale B' then that is already toggled on the next page but the fieldset has not changed? Is there any way to change to the jquery to detect which checkbox is toggled and change the fieldset accordingly or even modify the switch to make it work better?
Created a fiddle to show how the form works https://jsfiddle.net/v3waaa20/1/
Some slight changes and triggering change on load can do the trick.
$("input[name='sellorbuy']").change(function () {
value = $("input[name='sellorbuy']:checked").val();
$("#sell").toggle(value == "Yes");
$("#let").toggle(value == "No");
}).change();

Disable dropdown item based on selection done on other dropdown box

I am working with three dropdown selects with same options. I want to disable an item if that item is selected in other dropdown. I have JQuery code working for two dropdown box. i.e. if I am selecting one item its disabled in other dropdown. But I am having trouble doing it for three dropdown. If I select one item in first that item is disabled in two and three but when I select second dropdown, third dropdown only disables second dropdown selection.
I am new to jquery and AJAX.
PLease guide me.
Appreciate your help.
<select id="select1" name="phoneusescategories">
<option value="" selected="selected">top-1 use </option>
<option value="1"> Games</option>
<option value="2"> Productivity</option>
<option value="3"> News</option>
<option value="4"> Shopping & services</option>
<option value="5"> Music</option>
<option value="6"> Social Networking</option>
<option value="7"> Browser</option>
</select>
<select id="select2" name="phoneusescategories">
<option value="" selected="selected">top-2 use </option>
<option value="1"> Games</option>
<option value="2"> Productivity</option>
<option value="3"> News</option>
<option value="4"> Shopping & services</option>
<option value="5"> Music</option>
<option value="6"> Social Networking</option>
<option value="7"> Browser</option>
</select>
<select id="select3" name="phoneusescategories">
<option value="" selected="selected">top-3 use </option>
<option value="1"> Games</option>
<option value="2"> Productivity</option>
<option value="3"> News</option>
<option value="4"> Shopping & services</option>
<option value="5"> Music</option>
<option value="6"> Social Networking</option>
<option value="7"> Browser</option>
</select>
$(document).ready(function(){
$("select").change(function() {
$("select").not(this).find("option[value="+ $(this).val() + "]").attr('disabled', true);
});
});
http://jsfiddle.net/sakura1/xzjdsusz/
you might want to use prop() instead of attr() if you want to enable and disable the options of your select..
HERE: (jsFiddle DEMO)
I added this line just in case you want to remove other disabled options whenever you select a new option
$('select option').prop("disabled", false);
CODE SNIPPET :
$('select').on('change', function(){
$('select option').prop("disabled", false);
$("select").not(this).find("option[value="+ $(this).val() + "]").prop('disabled', true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<select id="select1" name="phoneusescategories">
<option value="" selected="selected">top-1 use </option>
<option value="Games"> Games</option>
<option value="2"> Productivity</option>
<option value="3"> News</option>
<option value="4"> Shopping & services</option>
<option value="5"> Music</option>
<option value="6"> Social Networking</option>
<option value="7"> Browser</option>
</select>
<select id="select2" name="phoneusescategories">
<option value="" selected="selected">top-2 use </option>
<option value="Games"> Games</option>
<option value="2"> Productivity</option>
<option value="3"> News</option>
<option value="4"> Shopping & services</option>
<option value="5"> Music</option>
<option value="6"> Social Networking</option>
<option value="7"> Browser</option>
</select>
<select id="select3" name="phoneusescategories">
<option value="" selected="selected">top-3 use </option>
<option value="Games"> Games</option>
<option value="2"> Productivity</option>
<option value="3"> News</option>
<option value="4"> Shopping & services</option>
<option value="5"> Music</option>
<option value="6"> Social Networking</option>
<option value="7"> Browser</option>
</select>
OK this is an updated answer
$('#select1,#select2,#select3').on('change', function(){
var value =$.trim($(this).val());
$('select option:contains("'+value+'")').attr('disabled','disabled');
});
New DEMO
it will work for just Games
So change your option value same of option text like so
<option value="Games"> Games</option>

Change the values of radio buttons depending on dropdown selection

Hi I have been trying to set radio buttons to certain values depending on which select drop down was chosen.
First the select statement:
<select id="department" name="department" class="input-xlarge" onchange="setDefault();">
<option value="" selected></option>
<option value="1">Read Only</option>
<option value="2">HR Staff</option>
<option value="3">Select 3</option>
<option value="4">Select 4</option>
<option value="7">Select 5</option>
</select>
I created a function that I thought would allow me to set the value, depenting on what is selected.
<script>
function setDefault() {
if (department.val() == '1'){
alert('One');
}
$("#radio-0").prop("checked", true);
</script>
I thought that if the val of department (the select drop down) was equal to 1 then set this value.
The error that I get in the console window is
*Uncaught TypeError: undefined is not a function*
I know this should be simple but as a jQuery novice I know I'm probably making a simple mistake.
Overall the goal would be to set a series of radio buttons to a specific value depending on the drop down selection. For example, if Read only was selected, certain values would be set as a 1, 2 ,3 4 or 5 (for a radio button) and so on depending on the drop down selection.
Thanks
Use jQuery onchange function.
$(document).ready(function () {
$("#department").change(function () {
if(this.value=='1')
{
alert("one");
}
$("#radio-0").prop("checked", true);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<select id="department" name="department" class="input-xlarge">
<option value="" selected></option>
<option value="1">Read Only</option>
<option value="2">HR Staff</option>
<option value="3">Select 3</option>
<option value="4">Select 4</option>
<option value="7">Select 5</option>
</select>
<input type="radio" id="radio-0">
I would recommend using javascript for this. It is such a small request that you don't really need an external libarary.
The HTML based from yours and including some radio buttons
<select id="department" name="department" class="input-xlarge" onchange="setDefault(this);">
<option value="" selected></option>
<option value="1">Read Only</option>
<option value="2">HR Staff</option>
<option value="3">Select 3</option>
<option value="4">Select 4</option>
<option value="7">Select 5</option>
</select><br />
<input type="radio" name="selectedop" id="radio-1" /><br />
<input type="radio" name="selectedop" id="radio-2" /><br />
<input type="radio" name="selectedop" id="radio-3" /><br />
<input type="radio" name="selectedop" id="radio-4" /><br />
<input type="radio" name="selectedop" id="radio-7" />
Then you simply add this javascript in your head. It selects the radio based on the value by combining "radio-" and the value to make the id.
function setDefault(input) {
document.getElementById("radio-" + input.value).checked = true;
}
JsFiddle Example: http://jsfiddle.net/bpmf3cks/
Try
$('#department').on('change',function() {
if ($(this).val() == '1') alert($(this).val())
})
http://jsfiddle.net/5x0h5g73/1/

Hiding/showing the select box options in jquery?

i have below code snippet in jsp
<HTML>
<BODY>
<select id="customerIds" onchange="doOperation()">
<option value="default"> Start..</option>
<div id="action1" class="action1">
<option value="1"> 1</option>
<option value="2"> 2</option>
<option value="3"> 3 </option>
</div>
<div id="action2" class="action2">
<option value="4"> 4 </option>
</div>
<option value="5"> 5 </option>
</select>
</BODY>
</HTML>
on click of certain button, i want to hide the options with id as "action1" and display the options with Id as "action2". So i tried this
$('#action1').hide();
$('#action2').show();
But that did not work.Not getting whats the issue? In firebug when i tried to inspect the select box, i did not find any div tag(i.e
with ids action1/action2 ) above options.
Use below javascript function where showOptionsClass is the class given to each option you want to show. This will
work in cross browser.
function showHideSelectOptions(showOptionsClass) {
var optionsSelect = $('#selectId');
optionsSelect.find('option').map(function() {
return $(this).parent('span').length == 0 ? this : null;
}).wrap('<span>').attr('selected', false).hide();
optionsSelect.find('option.' + showOptionsClass).unwrap().show()
.first().attr('selected', 'selected');
}
You may not have <div> elements within a <select>, see for example this stackoverflow on the topic, which references this bit of the HTML spec.
Further, hiding options isn't cross browser compatible (see this stackoverflow (second answer)), as #Voitek Zylinski suggests, you would probably be better off keeping multiple copies of the select and toggling between them, or if keeping the id attribute is required then maybe even adjusting the innerHtml (yuck...).
You could maybe approach it like:
markup
<select onchange="doOperation()" class="js-opt-a">
<option value="default"> Start..</option>
<option value="1"> 1</option>
<option value="2"> 2</option>
<option value="3"> 3 </option>
</select>
<select onchange="doOperation()" class="js-opt-b">
<option value="default">Start...</option>
<option value="4"> 4 </option>
<option value="5"> 5 </option>
</select>
js
function doOperation() { /*whatever*/}
$(".js-opt-a").hide();
$(".js-opt-b").show();​​
See for example this jsfiddle
Not exactly ideal though!
You can not use div to group but you can assign class to options to group them.
Live Demo
<select id="customerIds" onchange="doOperation()">
<option value="default"> Start..</option>
<option value="1" class="action1"> 1</option>
<option value="2" class="action1"> 2</option>
<option value="3" class="action1"> 3 </option>
<option value="4" class="action2"> 4 </option>
<option value="5" class="action3"> 5 </option>
</select>​
$('.action1').hide();
$('.action2').show();​
You can't group options inside a div. You can group them inside an <optgroup>, but I don't think that's what you're aiming for.
What you should do instead, is to either create two dropdowns that you toggle between or keep one dropdown and repopulate its options.
You can try this code :
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script>
$(document).ready(function(){
$("#hide").click(function(){
$('.action1').wrap('<span></span>').hide();
});
$("#show").click(function(){
$('.action1').unwrap('<span></span>').show();
});
});
</script>
</head>
<body>
<select id="customerIds" onchange="doOperation()">
<option value="default"> Start..</option>
<option value="1" class="action1"> 1</option>
<option value="2" class="action1"> 2</option>
<option value="3" class="action1"> 3 </option>
<option value="4" class="action2"> 4 </option>
<option value="5" class="action3"> 5 </option>
</select>​
<button id="hide">Hide</button>
<button id="show">Show</button>
</body>
</html>

Categories