Have a HTML / javascript :
<section class="on">
<select id="comboA" onchange="getComboA(this)">
<option value="[xfvalue_monthprice1]">100</option>
<option value="[xfvalue_monthprice2]">200</option>
<option value="[xfvalue_monthprice3]">300</option>
<option value="[xfvalue_monthprice5]">400</option>
</select>
<div id="prdiv">
<p id="prid"></p>
</div>
</section>
<script>
function getComboA(sel) {
var value = sel.value;
document.getElementById("prid").innerHTML = value;
}
</script>
Sections are more than 2. Class "on" of the section is dinamyc (on / off). How can I force the function to work only in section with "on" class?
Since you have tagged the question with jquery, You can use jquery to bind the change event only elements with a specific class name.
As the class name changes dynamically, you need to use delegation as well
$(document).on("change", "section.on select", function() {
$("#prid").html($(this).val());
});
I guess you need a javascript solution.
Here is my try:
HTML:
<section class="on">
<select id="comboA">
<option value="100">100</option>
<option value="200">200</option>
<option value="300">300</option>
<option value="400">400</option>
</select>
</section>
<section class="off">
<select id="comboB">
<option value="100">100</option>
<option value="200">200</option>
<option value="300">300</option>
<option value="400">400</option>
</select>
</section>
<div id="prdiv"><p id="prid"></p> </div>
JS:
var selCombo = document.querySelector(".on select");
selCombo.addEventListener("change", function() {
var value = this.value;
document.getElementById("prid").innerHTML = value;
});
FIDDLE
Related
This question already has answers here:
How do I programatically select an HTML option using JavaScript?
(11 answers)
Closed 1 year ago.
The code below is for a dropdown in html:
<html>
<body>
<select id="demo1" name="demo2" type="text">
<option value="london">London</option>
<option value="paris">Paris</option>
<option value="tokyo">Tokyo</option>
</select>
</body>
</html>
How to make the option with value "paris" selected?
Please consider it is not possible to change anything in the code above.
The code was edited to give more details because unfortunately the answers did not work on this question.
The below code did not work as well:
<script>
var selectedtext = "paris";
document.getElementById("demo1").selected = selectedtext;
</script>
If you know what position the option you want selected will always be in, you can use either of the following approaches:
To modify the DOM object property of the option, set the .selectedIndex property (which starts counting from 0) on the element.
document.querySelector("select").selectedIndex = 1;
<html>
<body>
<!-- A select element can't have a type attribute -->
<select id="demo1" name="demo2" type="text">
<option value="london">London</option>
<option value="paris">Paris</option>
<option value="tokyo">Tokyo</option>
</select>
</body>
</html>
If you do want to alter the HTML state of the element, you can use the setAttribute() method on the element:
document.querySelector("select").options[1].setAttribute("selected", "selected");
// Just for demo purposes:
console.log(document.querySelector("select").outerHTML);
<html>
<body>
<!-- A select element can't have a type attribute -->
<select id="demo1" name="demo2" type="text">
<option value="london">London</option>
<option value="paris">Paris</option>
<option value="tokyo">Tokyo</option>
</select>
</body>
</html>
But, if you only know the text of the option that should be selected and not its position, then you can use the following CSS selector with .querySelector to isolate the right option and select it:
let input = "paris";
// Either one of these will work:
// To affect the HTML state:
document.querySelector("option[value='" + input + "']").setAttribute("selected", "selected");
// To affect the DOM Object property:
document.querySelector("option[value='" + input + "']").selected = true;
// Just for demo purposes:
console.log(document.querySelector("select").outerHTML);
<html>
<body>
<!-- A select element can't have a type attribute -->
<select id="demo1" name="demo2" type="text">
<option value="london">London</option>
<option value="paris">Paris</option>
<option value="tokyo">Tokyo</option>
</select>
</body>
</html>
You can do this in multiple ways:
if the options might change order you select the value itself
document.querySelector('[value=paris]').selected = true
<select>
<option value="london">London</option>
<option value="paris">Paris</option>
<option value="tokyo">Tokyo</option>
</select>
Otherwise you can use selectIndex:
document.querySelector('select').selectedIndex = 1
<select>
<option value="london">London</option>
<option value="paris">Paris</option>
<option value="tokyo">Tokyo</option>
</select>
this way....
document.querySelector('select').value = 'paris';
<select>
<option value="london">London</option>
<option value="paris">Paris</option>
<option value="tokyo">Tokyo</option>
</select>
Or, with full use in a form:
const myForm = document.forms['my-form']
myForm.city.value = 'paris'
<form name="my-form">
<select name="city">
<option value="london">London</option>
<option value="paris">Paris</option>
<option value="tokyo">Tokyo</option>
</select>
</form>
<select id="nameList">
<option data-age="18" value="Jack">Jack</option>
<option data-age="28" value="John">John</option>
<option data-age="18" value="Robbin">Robbin</option>
<option data-age="38" value="Johnson">Johnson</option>
</select>
<div id="output"></div>
$(document).ready(function(){
$(document).on('change',"#nameList",function(){
var age = $("option:selected",this).attr('data-age');
$("#output").html(age);
});
});
How to implement this one in Angular2 ?
refer this link: https://jsfiddle.net/vqmnLk5u/
I need to remove some options from the 2nd dropdown menu based on the first.
I have tried quite a few iterations to get it done but still it is not working.
Kindly provide with solutions
In the below code I need to remove the option from the 2nd dropdown already selected in the 1st one.
I have tried the mentioned HTML & Javascript Code
HTML
<form>
Source:
<select id = "box1" name="a">
<option>select</option>
<option value="Apple">Apple</option>
<option value="Banana">Banana</option>
<option value="Carrot">Carrot</option>
</select>
Destination:
<select id="box2" name="b">
<option>select</option>
<option value="Apple">Apple</option>
<option value="Mango">Mango</option>
<option value="Dice">Dice</option>
<option value="Carrot">Carrot</option>
</select>
</form>
JAVASCRIPT
var main = function(){
$("#box1").change(function(){
var a = $('#box1 option:selected').val();
$("#box2 option[value='a']").remove();
});
}
$(document).ready(main);
I have tried entering the value as variable a with and without quotes but no effect. Although when i replaced 'a' with a specific value like 'Apple', then the code is working.
I think this is your answer ;)
var main = function(){
$("#box1").change(function(){
var a = $('#box1 option:selected').val();
$("#box2 option[value="+a+"]").hide().siblings().show();
});
}
$(document).ready(main);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select id="box1" name="b">
<option>select</option>
<option value="Apple">Apple</option>
<option value="Mango">Mango</option>
<option value="Dice">Dice</option>
<option value="Carrot">Carrot</option>
</select>
<select id="box2" name="b">
<option>select</option>
<option value="Apple">Apple</option>
<option value="Mango">Mango</option>
<option value="Dice">Dice</option>
<option value="Carrot">Carrot</option>
</select>
Hi kindly check https://jsfiddle.net/RRR0308/uubuumrz/1/
HTML
<select id="box1">
<option>111</option>
<option>222</option>
<option>333</option>
<option>444</option>
</select>
<select id="box2">
<option>999</option>
<option>222</option>
<option>888</option>
<option>444</option>
</select>
jQuery
$(document).ready(function(){
$('#box1').on('change', function(){
var x=$(this).val();
$('#box2 option').each(function(){
if($(this).val()==x){
$(this).remove();
}
});
});
});
hide().siblings().show()
instead of.remove()`
The .remove() permanently removes the selected option in box1 from box2. so if you use .hide() and .show(), it would hide and show. try making these changes.
$('#box1').change(function(){
var v = $(this).val();
$('#box2').find("option[value="+v+"]").remove(); });
Try this simple one
I want to build a drop down menu that the second selection will be displayed if the first selection data belongs to a specific category.
As you can see below, the first selection will be about COUNTRIES. If the country selected has states, then a second drop down selection will be displayed, containing the states of that country.
1)Is there a tag (in my code "xyz") that i can use it to separate the countries in "state" and "no-state" categories? If there is, how can i read the value of the "xyz" tag?
2) If i use the:
<option class="no-state" value="Germany">Germany</option>
and then use the jQuery to read it it will give me the value GermanySpain (which is correct but not what i want)
$('.no-state').val();
HTML PART
<div id="country">
<select>
<option xyz="no-state" value="Germany">Germany</option>
<option xyz="state" value="USA">USA</option>
<option xyz="no-state" value="Spain">Spain</option>
</select>
</div>
<div id="state" style="display:none" >
<select>
<option value="Utah">Utah</option>
<option value="New York">New York</option>
<option value="California">California</option>
</select>
</div>
JQUERY PART
$(document).ready(function(){
$('#country').change(function() {
if (the value of "xyz" tag is === 'no-state')
{
$('div#state').hide();
}
else
{
$('div#state').show();
}
});
});
What can i do to address this issue?
Thanks.
Added a variable to keep if a country has states or not according your custom attribute xyz
js
$(document).ready(function(){
$('#country').change(function() {
var hasStates = $(this).find("option:selected").attr("xyz");
if (hasStates == 'no-state')
{
$('div#state').hide();
}
else
{
$('div#state').show();
}
});
});
fiddle
I think you can make use of .data() jQuery method, which reads the data-* a valid html5 attribute, but you have to change your markup to fix and use this script:
$('#country select').change(function() {
if ($(this).find('option:selected').data('xyz') === 'no-state') {
$('div#state').hide();
} else {
$('div#state').show();
}
});
You have to add a data-* prefix to get to it and make it a valid html5 attribute.
<select>
<option data-xyz="no-state" value="Germany">Germany</option>
<option data-xyz="state" value="USA">USA</option>
<option data-xyz="no-state" value="Spain">Spain</option>
</select>
Using the class attribute isn't that bad:
HTML
<select>
<option class="no-state" value="Germany">Germany</option>
<option class="state" value="USA">USA</option>
<option class="no-state" value="Spain">Spain</option>
</select>
JavaScript
$(document).ready(function(){
$('#country').change(function() {
var $sel = $(this).find("option:selected");
if ($sel.hasClass("no-state"))
{
$('div#state').hide();
}
else
{
$('div#state').show();
}
});
});
Fiddle
First of all I would change your html structure to:
<select id="country">
<option xyz="no-state" value="Germany">Germany</option>
<option xyz="state" value="USA">USA</option>
<option xyz="no-state" value="Spain">Spain</option>
</select>
<select id="state" style="display: none;">
<option value="Utah">Utah</option>
<option value="New York">New York</option>
<option value="California">California</option>
</select>
Then you can simply do:
$("#country").change(function() {
var hasState = $(this).find(':selected').attr('xyz') === "state";
$("#state").toggle(hasState);
});
Here is a fiddle to see it in action.
These are Dynamic dependent select controls. The sets of values of (1-3, 4-6, 7-9) determine the use hide/show divs function. The problem is the function i have only hide/show depending on the div id. How can i make the function hide/show div depended on the values(1-3, 4-6, 7-9) found in the selectbox?
Jquery
$('#select').change(function() {
$('#sub1, #sub2, #sub3').hide();
$('#sub' + $(this).find('option:selected').attr('id')).show();
});
Html Setup
<html>
<select size="6" id="category">
<option value="">categories 1-3 </option>
<option value="">----</option>
<option value="">----</option>
</select>
<div id="sub1" style="display:none">
<select name="subc1" size="6">
<option value="1">subcategories 4-6</option>
<option value="2">---</option>
<option value="3">---</option>
</select>
</div>
<div id="sub2" style="display:none">
<select name="subc2" size="6">
<option value="4">subcategories 7-9</option>
<option value="5">----</option>
<option value="6">----</option>
</select>
</div>
<div id="sub3" style="display:none">
<select name="subc3" size="6">
<option value="7">End</option>
<option value="8">----</option>
<option value="9">----</option>
</select>
</div>
</html>
select the value from drop down change function and do the operation depends on the value of drop down, following is the sample code
$(function() // Shorthand for $(document).ready(function() {
$('select').change(function() {
if($(this).val() == 1)
{
$('#sub1').hide();
$('#sub2').show();
}
});
});
You nee to change minor change in your category select
<select size="6" id="category">
<option value="1">categories 1-3 </option>
<option value="2">----</option>
<option value="3">----</option>
</select>
$('select#category').on('change', function() {
$('div[id=^sub]:visible').hide();
$('div#sub' + this.value).show();
});
can also use .change() instead of .on('change').
$('select#category').change(function() {
$('div[id=^sub]:visible').hide();
$('div#sub' + this.value).show();
});
NOTE:
$('div[id=^sub]:visible') will point to all divs that have id start with sub and visible.
You are trying with $('#select') which need to be $('#category') or $('select#category').
According to your comment:
Complete solution will look like following:
function isOnlyDashed(text) {
return text.replace(/-/g, '').length === 0;
}
$('select#category').change(function() {
var text = $('option:selected', this).text();
if (!isOnlyDashed(text)) {
$('div[id=^sub]:visible').hide();
$('div#sub' + this.value).show();
}
});
$('select[name^=subc]').change(function() {
var text = $('option:selected', this).text();
if (!isOnlyDashed(text)) {
$(this).parent() // jump to parent div
.next('div[id^=sub]:hidden') // go to next hidden div
.show();
}
});
Complete Workout
The problem with your code is that you have an incorrect selector.
$('#select')...
should be
$('select')...
Also, this part is wrong
$('#sub' + $(this).find('option:selected').attr('id')).show();
Replace it with this
$('#sub' + $(this).val()).show();
Finally, having different elements with the same name/id "sub1" is probably a bad idea, though technically not illegal. It is certainly confusing.
working demo http://jsfiddle.net/FwBb2/1/
I have made minor changes in your Jquery code as well as added value in your first select drop-down list which was missing rest hope this helps.
Please lemme know if I missed anything! B-)
code
$('select').change(function() {
$('#sub1, #sub2, #sub3').hide();
$('#sub' + $(this).val()).show();
});
HTML
<html>
<select id="category">
<option value="1">categories 1-3 </option>
<option value="2">----</option>
<option value="3">----</option>
</select>
<div id="sub1" style="display:none">
<select name="subc1" size="6">
<option value="1">subcategories 4-6</option>
<option value="2">---</option>
<option value="3">---</option>
</select>
</div>
<div id="sub2" style="display:none">
<select name="subc2" size="6">
<option value="4">subcategories 7-9</option>
<option value="5">----</option>
<option value="6">----</option>
</select>
</div>
<div id="sub3" style="display:none">
<select name="subc3" size="6">
<option value="7">End</option>
<option value="8">----</option>
<option value="9">----</option>
</select>
</div>
</html>