javascript show or hide when option is changed - javascript

so i am trying to show or hide with javascript when option value K is pressed. When K is pressed div class "nobr" will appear. when option value K is not pressed "nobr" is hidden.
<select name="item[1502][additional_data] scustomg" style="width: 100%" onchange="setCustomPrice(1502, $(this))" id="select-custom-group">
<option value="1" data-price="300.0000">1 - €300.00</option>
<option value="2" data-price="210.0000">2 - €210.00</option>
<option value="4" data-price="189.0000">4 - €189.00</option>
<option value="6" data-price="210.0000">6 - €210.00</option>
<option value="8" data-price="210.0000">8 - €210.00</option>
<option value="9" data-price="0.0000">9 - €0.00</option>
<option value="K" data-price="0.0000">K - €0.00</option>
</select>
<div class="nobr">
<input type="checkbox" id="item_use_custom_price_1502" onclick="order.toggleCustomPrice(this, 'item_custom_price_1502', 'item_tier_block_1502');">
<label class="normal" for="item_use_custom_price_1502">Custom Price*</label>
</div>
I can get Javascript work and i See pop up box. but can get this when K is pressed.
<script>
var card = document.getElementById("select-custom-group");
if(card.selectedValue == "8") {
alert('select one answer');
}
else {
var selectedText = card.options[card.selectedIndex].text;
alert("Test");
}
</script>

with javascript you can add onchange function having the selected option as parameter, then check if the value is your required value, if it is the case then hide the div else show the div
<select name="item[1502][additional_data] scustomg" style="width: 100%" onchange="setCustomPrice('price_info_1', this)" id="select-custom-group">
<option value="1" data-price="">Pick a Price</option>
<option value="1" data-price="300.0000">1 - €300.00</option>
<option value="2" data-price="210.0000">2 - €210.00</option>
<option value="4" data-price="189.0000">4 - €189.00</option>
<option value="6" data-price="210.0000">6 - €210.00</option>
<option value="8" data-price="210.0000">8 - €210.00</option>
<option value="9" data-price="0.0000">9 - €0.00</option>
<option value="K" data-price="0.0000">K - €0.00</option>
</select>
<div class="nobr" id="price_info_1" style="display:none">
<input type="checkbox" onclick="">
<label class="normal" for="item_use_custom_price_1502">Custom Price*</label>
</div>
<br/>
<select name="item[1502][additional_data] scustomg" style="width: 100%" onchange="setCustomPrice('price_info_2', this)" id="select-custom-group">
<option value="1" data-price="">Pick a Price</option>
<option value="1" data-price="300.0000">1 - €300.00</option>
<option value="2" data-price="210.0000">2 - €210.00</option>
<option value="4" data-price="189.0000">4 - €189.00</option>
<option value="6" data-price="210.0000">6 - €210.00</option>
<option value="8" data-price="210.0000">8 - €210.00</option>
<option value="9" data-price="0.0000">9 - €0.00</option>
<option value="K" data-price="0.0000">K - €0.00</option>
</select>
<div class="nobr" id="price_info_2" style="display:none">
<input type="checkbox" onclick="">
<label class="normal" for="item_use_custom_price_1502">Custom Price*</label>
</div>
<script>
function setCustomPrice(div_to_toggle,selected){
if(selected.value=="K") document.getElementById(div_to_toggle).style.display = 'none';
else document.getElementById(div_to_toggle).style.display = 'block';
}
</script>

Obviously you need to listen to the change of the dropdown.
with jQuery, you can do
$('#select-custom-group').change(function () {
$('.nobr ').toggle($(this).val() === 'K');
});

Related

javascript variable calling not working on this following code

Those following code isnot working...
Firstly i want these selected value (by sentvalue() and receivevalue())
and then want to use those selected value on another(sent()) function ..
this is html code--
<select class="form-control" id="sents" onchange="sentvalue(this) " >
<option value="0" >-- Select One --</option>
<option value="1" ">1</option>
<option value="2" >2</option>
<option value="3" >3</option>
<option value="4" >4</option>
<option value="5" >5</option>
<option value="6" >6</option>
<option value="7" >7</option>
</select>
<select class="form-control" id="receives" onchange=" receivevalue(this); counter2(this); " >
<option value="0">-- Select One --</option>
<option value="1" onclick="i();">1</option>
<option value="2" >2</option>
<option value="3" >3</option>
<option value="4" >4</option>
<option value="5" >5</option>
<option value="6" >6</option>
<option value="7" >7</option>
</select>
<input type="number" id="sent2" onkeyup="sent()" class="form-control" name="number">
<p class="form-control" id="receive2" type="number" name="number" disabled>
i want to use receivevalue() and sentvalue() on sent() function but it does not work...
var x,y;
function receivevalue(selv) {
var y = selv.options[selv.selectedIndex].value;}
function sentvalue(selv) {
var x= selv.options[selv.selectedIndex].value;
}
function sent(){
receivevalue();
gett=y;
sentvalue();
putt=x;
if(gett==1 && putt==1){
var userinput=document.getElementById('sent2').value;
var news=(userinput/88).toFixed(2);
document.getElementById('receive2').innerHTML=news;
}
}
i want sentvalue() variable x and receivevalue() variable y on sent() function . please solve my problem..
function sent(){
rselv = document.getElementById('receives');
receivevalue(rselv);
get=y;
sselv = document.getElementById('sents')
sentvalue(sselv);
put=x;
if(get==1 && put==1){
var userinput=document.getElementById('sent2').value;
var news=(userinput/88).toFixed(2);
document.getElementById('receive2').innerHTML=news;
}
}
First of all there is typo in sentevalue. It should be sentvalue.
Then your x, y variables are global. You shouldn't re-declare them inside function.
var x,y;
function receivevalue(selv) {
y = selv.options[selv.selectedIndex].value;
}
function sentvalue(selv) {
x = selv.options[selv.selectedIndex].value;
}
Now since you have values x and y in global variables, sent function can simply be written like this.
function sent(){
if(x==1 && y==1){
var userinput=document.getElementById('sent2').value;
var news=(userinput/88).toFixed(2);
document.getElementById('receive2').innerHTML=news;
}
}
And Correct HTML is:
<select class="form-control" id="sents" onchange="sentvalue(this)">
<option value="0" >-- Select One --</option>
<option value="1" >1</option>
<option value="2" >2</option>
<option value="3" >3</option>
<option value="4" >4</option>
<option value="5" >5</option>
<option value="6" >6</option>
<option value="7" >7</option>
</select>
<select class="form-control" id="receives" onchange="receivevalue(this); counter2(this);">
<option value="0">-- Select One --</option>
<option value="1" onclick="i();">1</option>
<option value="2" >2</option>
<option value="3" >3</option>
<option value="4" >4</option>
<option value="5" >5</option>
<option value="6" >6</option>
<option value="7" >7</option>
</select>
<input type="number" id="sent2" onkeyup="sent()" class="form-control" name="number">
<p class="form-control" id="receive2" type="number" name="number" disabled>

javascript dependent drop down with doctrine form element

i'm trying to do a drop down list using javascript jquery.
With zend framework2 and doctrine i rendered html elements as follows:
<div>
Country<select name="country"><option value="">--select Country-</option>
<option value="1" class="country" id="country">Greater Accra</option>
<option value="2" class="country" id="country">Ashanti</option>
<option value="3" class="country" id="country">Central</option>
<option value="4" class="country" id="country">Eastern</option></select>
</div>
<div>
States<select name="states"><option value="">--select States--</option>
<option value="1" class="states" id="states" data-id="1">Roman Ridge</option>
<option value="2" class="states" id="states" data-id="2">Kumasi</option>
<option value="3" class="states" id="states" data-id="3">Cape Coast</option>
<option value="4" class="states" id="states" data-id="4">Koforidua</option>
<option value="5" class="states" id="states" data-id="1">Accra</option>
<option value="6" class="states" id="states" data-id="3">Moree</option>
<option value="7" class="states" id="states" data-id="1">Chantan</option>
<option value="8" class="states" id="states" data-id="1">Achimota</option>
<option value="9" class="states" id="states" data-id="2">Mampong</option>
<option value="10" class="states" id="states" data-id="1">Lapaz</option></select>
</div>
and this is my jquery fuction
<script>
$(document).ready(function(){
var s1 = $('#country');
var s2 = $('#states');
var dependantOptions = s2.find('option');
dependantOptions.css('visibility', 'hidden');
s1.on('change', function() {
dependantOptions.css('visibility', 'hidden');
s2.find("option[data-id='" + $(this).val() + "']").css('visibility', 'visible');
});
});
</script>
i cant figure out why the javascript function isn't working.
i hope to find an answer soon. thanks
Your select list have only the attributes name="country" and name="states" and no corresponding ids. You search like this:
var s1 = $('#country');
var s2 = $('#states');
So you should add the ids id="country" and id="states".

List item and statement html javascript

I have a html code (a list item), and it works fine, when you click it, it drops down the list item, here is the code:
<div class="list-item"> <strong class="table-cell">Choose:</strong> <span class="table-cell right">
<!-- <input type="text" name="numberofchildren" id="numberofchildren" value=""> -->
<select name="numberofchildren" id="numberofchildren" value="numberofchildren" >
<option value="Blood Group" selected disabled>NO CHILDREN</option>
<option value="No Children" >No Children</option>
<option value="1" >1</option>
<option value="2" >2</option>
<option value="3" >3</option>
<option value="4" >4</option>
<option value="5" >5</option>
<option value="6" >6</option>
<option value="7" >7</option>
<option value="8" >8</option>
<option value="9" >9</option>
<option value="10" >10</option>
</select>
</span> </div>
But my question is this, how can I add a javascript code, that when a user chooses, for example "1" ( if he chose == "1" {...}, how can I do that.
Hope that I was clear, thank you very much for your time and effort.
function myfunction(select) {
if (select.value == "1") {
alert("You selected ONE");
} else {
alert("You selected: " + select.value);
}
}
<select name="numberofchildren" id="numberofchildren"
value="numberofchildren" onchange="myfunction(this)">
<option value="">Please select value...</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>

How to calculate value from select fields?

I took the example from here http://tympanus.net/codrops/2012/11/29/simple-effects-for-drop-down-lists/, Demo 4
Everything works fine, however I can not figure out how to calculate the sum from select list values and to withdraw at the end of the page. I try to do it with this code but it not work `
<section class="main clearfix">
<div class="fleft">
<select id="cd-dropdown" class="cd-select">
<option value="-1" selected>Choose a weather condition</option>
<option value="1" class="icon-sun">Sun</option>
<option value="2" class="icon-cloudy">Clouds</option>
<option value="3" class="icon-weather">Snow</option>
<option value="4" class="icon-rainy">Rain</option>
<option value="5" class="icon-windy">Windy</option>
</select>
</div>
<input type="text" id="sum">
</section>
$( function() {
$( '#cd-dropdown' ).dropdown( {
gutter : 5,
delay : 40,
rotated : 'left'
} );
});
</script>
<script type="text/javascript">
$("#cd-dropdown").change(function() {
var total = 0;
$.each($(".cd-select") ,function() {
total += parseInt($(this).val());
});
$("#sum").val(total)});
`
Thank you in advance !
$(document).ready(function () {
$(".cd-select").change(function () {
total = 0;
$('.cd-select').each(function () {
if ($(this).val() != "-1") {
total = total + parseInt($(this).val(), 10);
}
});
$('#sum').val(total);
});
});
<div class="fleft">
<select id="cd-dropdown" class="cd-select">
<option value="-1" selected>Choose a weather condition</option>
<option value="1" class="icon-sun">Sun</option>
<option value="2" class="icon-cloudy">Clouds</option>
<option value="3" class="icon-weather">Snow</option>
<option value="4" class="icon-rainy">Rain</option>
<option value="5" class="icon-windy">Windy</option>
</select>
<select id="Select1" class="cd-select">
<option value="-1" selected>Choose a weather condition</option>
<option value="1" class="icon-sun">Sun</option>
<option value="2" class="icon-cloudy">Clouds</option>
<option value="3" class="icon-weather">Snow</option>
<option value="4" class="icon-rainy">Rain</option>
<option value="5" class="icon-windy">Windy</option>
</select>
<select id="Select2" class="cd-select">
<option value="-1" selected>Choose a weather condition</option>
<option value="1" class="icon-sun">Sun</option>
<option value="2" class="icon-cloudy">Clouds</option>
<option value="3" class="icon-weather">Snow</option>
<option value="4" class="icon-rainy">Rain</option>
<option value="5" class="icon-windy">Windy</option>
</select>
</div>
<input type="text" id="sum">
When any select with class 'cd-select' change the sum will be calculated and displayed on the text field
Summation is not being made as you expect because your total variable is reset to 0 every time the event listener is fired. Thus your code will work if you define that variable as global.
Thus what you want is:
var total = 0;
$("#cd-dropdown").change(function() {
$.each($(".cd-select"), function() {
total += parseInt($(this).val());
});
$("#sum").val(total)
});

onchange select box

Say we have 2 different select boxes with the same number of options:
<select id="box1">
<option value="1" >1</option>
<option value="2" >2</option>
</select>
<select id="box2">
<option value="3" >3</option>
<option value="4" >4</option>
</select>
Is it possible when we select the first box to select the same option number in the second box automatically? Without jQuery if possible.
UPDATE: the values of 2 boxes are different! When we select the first value of box1 the box 2's value will automatically would be 3! and so on...
Yes. Add an onChange property to the first select, then use it to call a javascript function you have written elsewhere.
<select id="box1" onChange="changeBox2(this.selectedIndex);">
<option value="1" >1</option>
<option value="2" >2</option>
</select>
<select id="box2">
<option value="1" >1</option>
<option value="2" >2</option>
</select>
W3 Schools JavaScript Events
Example on Accessing SELECT properties in JS
The following JS would work:
document.getElementById("box1").onchange = function(){
document.getElementById("box2").value = this.value;
};
Not the most elegant code I've written but gets you what you need. Fiddle: http://jsfiddle.net/XZRKS/
A javascript snippet code:
var box1=document.getElementById("box1");
box1.onchange=function (){
document.getElementById("box2").value=box1.value;
}
Hope below is what you want...
<html>
<body>
<select id="box1" onChange="box2.value=box1.value">
<option value="1" >1</option>
<option value="2" >2</option>
<option value="3" >3</option>
<option value="4" >4</option>
<option value="5" >5</option>
<option value="6" >6</option>
<option value="7" >7</option>
</select>
<select id="box2" onChange="box1.value=box2.value">
<option value="1" >1</option>
<option value="2" >2</option>
<option value="3" >3</option>
<option value="4" >4</option>
<option value="5" >5</option>
<option value="6" >6</option>
<option value="7" >7</option>
</select>
</body>
</html>
var box1 = document.getElementById("box1");
var box2 = document.getElementById("box2");
box1.addEventListener("change", function(e){
var val = parseInt(this.value);
box2.querySelector("option:nth-child("+val+")").selected = true;
}, false);
This will only work in Gecko/Mozilla only
For IE use onChange instead of change and attachEvent instead of addEventListener, for more info visit
http://msdn.microsoft.com/en-us/library/ie/ms536343%28v=vs.85%29.aspx
for information on querySelector see this link
https://developer.mozilla.org/en-US/docs/Web/API/document.querySelector
$("#box1").on('change',function(){
document.getElementById("box2").value = this.value;
console.log($("#box2").val());
});
$("#box2").on('change',function(){
document.getElementById("box1").value = this.value;
});
<select id="box1">
<option value="1" >1</option>
<option value="2" >2</option>
</select>
<select id="box2">
<option value="1" >1</option>
<option value="2" >2</option>
</select>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
yes it's simple in javascript. just one line.
function ch(){
document.getElementById("box2").value = document.getElementById("box1").value ;
}
you can call ch() on onchange event of box2

Categories