Java Script DrobDown Disable Mark - javascript

Here is my question, for exp:
<select name="select1" >
<option value="default" selected="selected">Default</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<select name="select2" >
<option value="default" selected="selected">Default</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
If I select (number 1) from (select1), then (select2) totally will be disabled. or if i select (number4) from (select2) then (select1) totally will be disabled.
I mean depends on select name, other select name will be disabled.
Is it possible to give me the java code for this ?
Thank you

Using jquery, it'd be something like this. jsfiddle
HTML:
<select id="select1" name="select1" >
<option value="default" selected="selected">Default</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<select name="select2" id="select2" >
<option value="default" selected="selected">Default</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
JS:
$('#select1').on('change', function(){
if($(this).val() != 'default'){
$('#select2').prop('disabled', true);
}
else{
$('#select2').prop('disabled', false);
}
});
$('#select2').on('change', function(){
if($(this).val() != 'default'){
$('#select1').prop('disabled', true);
}
else{
$('#select1').prop('disabled', false);
}
});
As I said, you need to correct your html as well. In your website, use the following html instead of what you have i.e. add ID's to your select dropdown.
<select id="select1" name="select1" ...
<select id="select2" name="select2" ...

Related

Get alerted when the value of select (dropdown) became empty

I have a dropdown that has a couple of options one of them is empty, the same as the following code bellow.
I want to write a jquery code that keep tracking the dropdown and console.log("empty") when i select the empty option then console.log("not empty") when i choose anything else
The HTML code:
<select id="month">
<option value=""></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
Thanks
You could use Vanilla JS do to it, like this:
let month = document.querySelector("#month");
month.addEventListener("change", (e)=>{
if(e.target.value === ""){
console.log("Empty");
}else{
console.log("Not Empty");
}
});
console.log(month.value);
<select id="month">
<option value=""></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
Was that what you wanted?
function myFunction(){
if ($('#month').find(":selected").val() != '') console.log('Not Empty')
else console.log('Empty')
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="month" onchange="myFunction()">
<option value=""></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>

How to check if same option is selected in a dynamically created form rows

Been trying for hours but cannot get it right how can i check if an option is already selected in another select. Where am creating my form rows dynamically with the same class for all the newly created select.
below is how my form is after being created dynamically.
<select class="tank_id" name="Form[tank_id][]">
<option value="">Select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select class="tank_id" name="Form[tank_id][]">
<option value="">Select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select class="tank_id" name="Form[tank_id][]">
<option value="">Select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
what i want to achieve is to make sure the data submitted does not have duplicates. My issue is to find out if an option is already selected in a previous select below is the js i have tried but haven't achieved anything with it.
$(".tank_id").on('change', function(){
var inputs = $('.tank_id');
var result = inputs.filter(function(i,el){
return inputs.not(this).filter(function() {
return this.value === el.value;
});
});
if (result.length > 0) {
alert('match found')
}
});
Try this
$(".tank_id").on('change', function() {
var current = $(this);
var inputs = $('.tank_id').not(this);
var result = inputs.filter(function(i, el) {
return $(current).prop("selectedIndex") === $(el).prop("selectedIndex");
});
if (result.length > 0) {
alert('Error: Selected Already')
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<select class="tank_id" name="Form[tank_id][]">
<option hidden>Select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select class="tank_id" name="Form[tank_id][]">
<option hidden>Select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select class="tank_id" name="Form[tank_id][]">
<option hidden>Select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
This will only check if the value you're currently selecting is already selected in another list. So if you have selected 2 in the first two dropdowns, and then selects 1 in the last, the last won't give an error message.
$(".tank_id").on('change', function() {
var currentValue = $(this).val();
var inputs = $('.tank_id').not(this);
var matches = inputs.filter(function() {
return $(this).val() === currentValue;
});
if (matches.length > 0) {
alert('The current selectbox value is already selected somewhere else!');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="tank_id" name="Form[tank_id][]">
<option value="">Select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select class="tank_id" name="Form[tank_id][]">
<option value="">Select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select class="tank_id" name="Form[tank_id][]">
<option value="">Select</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>

If Select tag option is selected shows another select tag [duplicate]

This question already has answers here:
Populating One Select Box Based on the Selection in Another Select Box - JQuery?
(3 answers)
Closed 8 years ago.
I need code either in js or jquery or php like this.
<select id="1">
<option value="1">....</option>
<option value="2">....</option>
<option value="3">....</option>
</select>
If option value 3 is selected, it should populate another select like
<select id="2">
-
-
</select>
if not value 3 is selected nothing happens.
You can do this by hiding and showing the other select depending on the value that is chosen (unless you want to populate the results dynamically after clicking option 3 in which case you will have to be more specific)
HTML
<select id="select1">
<option>Choose One</option>
<option value="1">Val 1</option>
<option value="2">Val 2</option>
<option value="3">Val 3</option>
</select>
<select id="select2">
<option>Choose Two</option>
<option value="1">Val 1</option>
<option value="2">Val 2</option>
<option value="3">Val 3</option>
</select>
CSS
#select2{
display: none;
}
JS
$("#select1").change(function(){
if($(this).val() == 3){
$("#select2").show();
}else{
$("#select2").hide();
}
});
FIDDLE
Here is a PURE Javascript Solution
See this fiddle
Javascript
function myFunction() {
var x = document.getElementById("id1").value;
if (x == "3") document.getElementById("id2").style.display = "block";
}
HTML
<select id="id1" onchange="myFunction()">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select id="id2">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
CSS
#id2 {
display:none;
}
Use this
Html
<select id="1">
<option value="1">....</option>
<option value="2">....</option>
<option value="3">....</option>
</select>
<select id="2" style="display:none;">
<option value="1">....</option>
<option value="2">....</option>
<option value="3">....</option>
</select>
Your Jquery Here
$("#1").change(function(){
if($(this).val() == 3){
$("#2").show();
}else{
$("#2").hide();
}
});
Try this..
<select id="1" class="first">
<option value="1">....</option>
<option value="2">....</option>
<option value="3">....</option>
</select>
<select id="2" style="display:none;">
<option value="1">....</option>
<option value="2">....</option>
<option value="3">....</option>
</select>
$(".first").change(function(){
var selectvalue=$(".first").val();
if(selectvalue==3)
{
$("#2").show();
} else {
$("#2").hide();
}
});

How to enable/disable drop down list box in HTML + Javascript/JQuery?

I have 2 drop down lists of pairs of name IMEI's and cities, as following:
<HTML>
<body>
Select Programming font:
<select name="IMEI">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">$</option>
</select>
<select name ="city">
<option value="jhansi">Jhansi</option>
<option value="Delhi">Delhi</option>
<option value="Mumbai">Mumbai</option>
<option value="Kanpur">Kanpur</option>
</select>
</body>
</html>
The desired behavior is: when one selects a city name, the IMEI list enables; otherwise it is disabled.
How can I achieve this?
Thanks in advance!
Fiddle Demo
$(function () {
var sel_imie = $('select[name="IMEI"]');
sel_imei.prop('disabled', true); //disable IMEI select
$('select[name ="city"]').change(function () {
sel_imei.prop('disabled', false); //enable when value of city select is changed
});
});
.change()
Try this,
<select name="IMEI" disabled="disabled" id="IMEI">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">$</option>
</select>
<select name ="city" id="city">
<option value="jhansi">Jhansi</option>
<option value="Delhi">Delhi</option>
<option value="Mumbai">Mumbai</option>
<option value="Kanpur">Kanpur</option>
</select>
Script
$('#city').on('change',function(){
$('#IMEI').attr('disabled',false);
});
Demo:http://jsfiddle.net/khmSm/
Select Programming font:
<select name="IMEI" disabled="disabled">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">$</option>
</select>
<select name="city">
<option value="">Select...</option>
<option value="jhansi">Jhansi</option>
<option value="Delhi">Delhi</option>
<option value="Mumbai">Mumbai</option>
<option value="Kanpur">Kanpur</option>
</select>
document.getElementsByName('city')[0].addEventListener('change', function(e){
var imei = document.getElementsByName('IMEI')[0];
var city = e.target;
imei.disabled = city.value != '' ? '' : 'disabled';
});
try this way
HTML CODE:
CITY :<select name="city">
<option value="jhansi">Jhansi</option>
<option value="Delhi">Delhi</option>
<option value="Mumbai">Mumbai</option>
<option value="Kanpur">Kanpur</option>
</select>
<br/>
IMEI :<select name="IMEI">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">$</option>
</select>
JQUERY CODE:
$('select[name=IMEI]').prop('disabled',true);
$('select[name=city]').on('change',function () {
$('select[name=IMEI]').prop('disabled',false);
});
LIVE DEMO:
http://jsfiddle.net/dreamweiver/TvpRF/7/
Happy Coding :)
<select name="IMEI" disabled="disabled" id="IMEI">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">$</option>
</select>
<select name ="city" id="city">
<option value ="choose">Choose a city</option>
<option value="jhansi">Jhansi</option>
<option value="Delhi">Delhi</option>
<option value="Mumbai">Mumbai</option>
<option value="Kanpur">Kanpur</option>
</select>
Here is the link : http://jsfiddle.net/khmSm/1/
Disable select and update the plugin. Use trigger chosen:updated to disable the select widget.
$('#RefundType').prop('disabled', true).trigger("chosen:updated");

Getting value from HTML select tag

I have a html combo/select. When the user selects a element, I want to populate textboxcontrol from it. How to do it.
HTML:
<select id="combobox">
<option value="">Pick one</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<input id="textbox" type="text" />
Javascript (assumes jQuery):
$('#combobox').change( function(){
$('#textbox').val( $(this).val() );
}).click( function(){
$('#textbox').val( $(this).val() );
});

Categories

Resources