How to Change select dropdown value while selecting downdown 1? - javascript

I have the db called rec where i store records like:
Id Name Surname
1 John smith
2 Roger jhonson
I wish to display all records in 2 drop-down menus. Say if I select Roger then the value of surname should be changed to Jhonson if it was already on Smith.
If I select John then value of the surname should go back to smith but if I want I should be able to change the surname of John to Jhonson which should not change the value of Name, the selected value should remain as John Jhonson.
Can someone please show me how this can be done?
Really appreciate your kind help and time in advance.

HTML
<select name="cname" class="float-right ralign" id="cname">
<option value="" disabled selected>--select name--</option>
<option value="1">John</option>
<option value="2">Roger</option>
</select>
<select name="surname" class="float-right ralign" id="surname">
<option value="" disabled selected>--select surname--</option>
<option value="1">smith</option>
<option value="2">jhonson</option>
</select>
Use jQuery Onchange Event
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript">
$( document ).ready(function(){
$("#cname").change(function(){
var cname = $(this).val();
$('#surname option').eq(cname).prop('selected', true);
});
});
$( document ).ready(function(){
$("#cname").change(function(){
var cname = $(this).val();
$('#surname option').eq(cname).prop('selected', true);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<select name="cname" class="float-right ralign" id="cname">
<option value="" disabled selected>--select name--</option>
<option value="1">John</option>
<option value="2">Roger</option>
</select>
<select name="surname" class="float-right ralign" id="surname">
<option value="" disabled selected>--select surname--</option>
<option value="1">smith</option>
<option value="2">jhonson</option>
</select>

Related

Opencart change default value of list

I have a list where you can choose an option. I need to change default value to my own. Is it possible in Opencart?
<div class="form-group required" id="form1">
<label class="control-label" for="input-option227">Choose</label>
<select name="option[227]" id="frmdocumentsForm-r1" class="form-control" wtx-context="0128A83E-7CFB-40AA-836C-8D17569841BF">
<option value=""> --- Please choose an option --- </option>
<option value="17">AAAA</option>
<option value="18">BBB</option>
<option value="19">CCC</option>
<option value="44">DDD</option>
</select>
</div>
I need to change value "17" -> "AAA"
Thanks
change attribute value with loop . If you need replace only 1st value 17 check for if condition.
$( "select > option").each(function() {
$(this).attr('value',$(this).text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
<option value=""> --- Please choose an option --- </option>
<option value="17">AAAA</option>
<option value="18">BBB</option>
<option value="19">CCC</option>
<option value="44">DDD</option>
</select>

Add a text field to html dropdown

I have a html dropdown field where there should be a text field added at the bottom naming other.The text typed in this field should come as other:text
drop down html is :
<select name="drop">
<option selected="selected">Please select ...</option>
<option value="car">car</option>
<option value="bike">bike</option>
<option value="Other">Other</option>
</select>
Here other should be text field.For understanding i kept other as option,but i want that to be a input text field where value is given by user.
Thanks in advance
You could add hidden input field by default for other text :
<input type='text' id='other' name='other' style="display:none"/>
And capture the change on select in your js then check if selected option value equal Other and show the field or hide it :
$('select').on('change', function(){
if($(this).val()=='Other'){
$('#other').show().focus();
}else{
$('#other').val('').hide();
}
})
Hope this helps.
$('select').on('change', function(){
if($(this).val()=='Other'){
$('#other').show().focus();
}else{
$('#other').val('').hide();
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="drop">
<option selected="selected">Please select ...</option>
<option value="car">car</option>
<option value="bike">bike</option>
<option value="Other">Other</option>
</select>
<input type='text' id='other' name='other' style="display:none" value='other:'/>
<script type="text/javascript">
function showfield(name){
if(name=='Other')document.getElementById('div1').innerHTML='Other: <input type="text" name="other" />';
else document.getElementById('div1').innerHTML='';
}
</script>
<select name="drop" id="drop" onchange="showfield(this.options[this.selectedIndex].value)">
<option selected="selected">Please select ...</option>
<option value="car">car</option>
<option value="bike">bike</option>
<option value="Other">Other</option>
</select>
<div id="div1"></div>
you could go the other way and make all options accessible by the text field - but giving an autocomplete option as the user types. This can be done with jQuery autocomplete plugins etc or the new html5 datalist. This ties a predetermined set of options to the textfield - allowing the user to select from it if theinput value matches the value, but also allows for alternative text to be entered if there are no matches. Just note though that Safari (and possible other browsers) do not support this feature yet. But its very cool when it is supported.
<input id="transportOptions" list="transport">
<datalist id="transport">
<option value="Car">
<option value="Bike">
<option value="Scooter">
<option value="Taxi">
<option value="Bus">
</datalist>

How to validate multiple drop down list in jquery

HTML :
<select type="text" class="que_ans" name="answer[12]" id="answer_12" size="1">
<option value="0" selected> -- Select Response -- </option>
<option value="1">Not Satisfied</option>
<option value="2">Somewhat Satisfied</option>
</select>
<select type="text" class="que_ans" name="answer[13]" id="answer_13" size="1">
<option value="0" selected> -- Select Response -- </option>
<option value="1">Not Satisfied</option>
<option value="2">Somewhat Satisfied</option>
</select>
How to validate the drop down list using array name answer[12] ?
Add class and title attributes on your dropdown element like:
<select type="text" class="que_ans required" name="answer[12]" id="answer_12" size="1" title="This is required field" >
<option value="0" selected> -- Select Response -- </option>
<option value="1">Not Satisfied</option>
<option value="2">Somewhat Satisfied</option>
</select>
<select type="text" class="que_ans required" name="answer[13]" id="answer_13" size="1" title="This is required field">
<option value="0" selected> -- Select Response -- </option>
<option value="1">Not Satisfied</option>
<option value="2">Somewhat Satisfied</option>
</select>
And add following script at the end of your page
<script>
$('select.required').each(function () {
var message = $(this).attr('title');
if($(this).val() == '' || $(this).val() == 0) {
alert(message);
$(this).focus();
breakout = true;
return false;
}
}
});
</script>
I hope, it will fulfill your requirement.
you could use attribute selector $(select[name="answer[12]"]) for specific ones
or more generic $(select[name]) to select all <select> with name attribute.
also val() method can be used for the getting the selected item.
eg: $(select[name="answer[12]"]).val()

Insert country code to phone input field with jQuery

In a "contact us" form, I need to insert a country code (ie 972, 1, 91) to the phone input field after choosing the country from the select list.
For example;
If a user chooses "US" from the list, than the phone input field will start with the number "1" and than the user will continue to write his number. How do I do that with jQuery?
Thank you
You can do this buy adding the value as code of the Country like for US the value of option should be 1
Your HTML should be,
<select id="country">
<option value="">Select Country</option>
<option value="1">US</select>
<option value="91">IN</select>
</select><br/>
Code: <input type="text" id="code" />
Jquery
$(function(){
$('#country').on('change',function(){
$('#code').val(this.value);// changing the code textbox value by current country value
});
});
HTML:
<select id="country">
<option value="">Select Country</option>
<option value="1">US</option>
<option value="+44 ">UK</option>
</select>
<input id="phone" type="text" name="phone">
jQuery:
$(function() {
$('#country').on('change', function() {
$('#phone').val($(this).val());
});
});
<select id="country">
<option value="">Select Country</option>
<option value="1">US</option>
<option value="91">IN</option>
</select>
<br><br>
Code: <input type="text" id="code" />
$(function() {
$('#country').on('change', function() {
$('#phone').val($(this).val());
});
});`enter code here`

Multiple show / hide jquery

My code below is showing the proper city/state div's when the matching country is selected from the drop down, but it's not hiding the previous one if the user decides to select another state. Do I have to create an if/else each for each div i want to hide?
<script type="text/javascript">
$(document).ready(function() {
...
//city & state display
$("#ctry").change(function() {
$(".state").hide();
var stateSelect = $("#state_" + $(this).val());
if (stateSelect.length === 0)
$("#state_label").hide();
else {
$("#state_label").show();
stateSelect.show();
}
});
});
</script>
html code:
<label>Country/Locale:</label>
<select id="ctry" name="country">
<option value="">-- Select</option>
<option value="US">United States</option>
<option value="CA">Canada</option>
<option value="GB">United Kingdom</option>
<option value="AU">Australia</option>
</select><br />
<!-- US -->
<div id="state_US" style="display:none">
<label id="state_label" style="display:none">State:</label>
<span class="rstate">
<select name="u_state">
<option value="">-- State US</option>
<option value="AL">Alabama</option>
</select>
</span>
Zip or City:<input style="margin-left:43px;" type="text" name="locus" id="locus" size="30" />
</div>
<!-- CA -->
<div id="state_CA" style="display:none">
<label id="state_label" style="display:none">Province:</label>
<span class="rstate">
<select name="c_state">
<option value="">-- Prov CA</option>
<option value="ON">Windsor</option>
</select>
</span>
Postal or Place:<input style="margin-left:43px;" type="text" name="locca" id="locca" size="30" />
</div>
<!-- GB -->
<div id="state_GB" style="display:none">
<label id="state_label" style="display:none">City or Region:</label>
<span class="rstate">
<select name="k_state">
<option value="">-- Place</option>
<option value="AU">UK!</option>
</select>
</span>
</div>
<!-- AU -->
<div id="state_AU" style="display:none">
<label id="state_label" style="display:none">City or Region:</label>
<span class="rstate">
<select name="a_state">
<option value="">-- Place</option>
<option value="AU">UK!</option>
</select>
</span>
</div>
Remove the state_label ids..
Add the unused class state to the state_CA, state_US etc. elements..
So each state group should be
<div id="state_US" class="state" style="display:none">
<label style="display:none">State:</label>
<span class="rstate">
<select name="u_state">
<option value="">-- State US</option>
<option value="AL">Alabama</option>
</select>
</span>
Zip or City:<input style="margin-left:43px;" type="text" name="locus" id="locus" size="30" />
</div>
And change your script to be
<script type="text/javascript">
$(document).ready(function() {
...
//city & state display
$("#ctry").change(function() {
$(".state").hide();
var stateSelect = $("#state_" + $(this).val());
stateSelect.show();
});
});
</script>
Demo at http://jsfiddle.net/gaby/PYx2v/
You cannot have multiple elements with the same ID. Your label state_Label is not unique. You should change this, otherwise some browsers will not show your label.
You hide all elements having the css-class "state". But there is no element having this class. I guess that you need to add class="state" to all of your state_*-divs.
Something like this logic might work for you:
var previous;
$(document).ready(function() {
$("#ctry").change(function() {
$(".state").hide();
var stateSelect = $("#state_" + $(this).val());
if (stateSelect.length === 0) {
$("#state_label").hide();
} else {
if (previous) {
previous.hide(function() {
$("#state_label").show();
stateSelect.show();
previous = stateSelect;
});
} else {
$("#state_label").show();
stateSelect.show();
previous = stateSelect;
}
}
});
});
Though it would be easier if they all had the same CSS class, with which you could easily hide them and then show the one that was selected.

Categories