Change date based on select value/month/year - javascript

I want to change date based on selected value
$(document).on('change', '.payment_options', function () {
var value = $('#value_select').val();
var data = $('#per_month_year').val();
var d = new Date();
d.setMonth(d.getMonth() + value);
var subs_till = d.toDateString();
if (data == 'month') {
expire_date = subs_till;
} else {
//if data == year, change the membership valid till the value enter: like 2 years, 3 years etc
}
$('#membership_till_date').html(expire_date);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label>Choose Value</label>
<select name="" class="form-control payment_options" id="value_select">
<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>
<option value="11">11</option>
<option value="12">12</option>
</select>
<label>Choose month/year</label>
<select name="" class="form-control payment_options" id="per_month_year">
<option value="month">Month(s)</option>
<option value="year">Year(s)</option>
</select>
<br>
<small>Membership valid from 'Today' to <span id="membership_till_date">"this have to change after choosing value/month-year"</span></small>
But the problem is it gives +5 year in the result, I want to display exactly the data like, if they choose value-data then display "till +5 month form now", "2 years form now", etc in the membership valid sections

When you get the value of a text field, you always get a String back.
If, for example, you choose the number 2 in the value_select field your code will return a String value of "2".
var value = $('#value_select').val();
So you need to convert the String value to an integer, which you can do like this:
var value = parseInt($('#value_select').val());
Your code then works

Related

I want disable time (before now) in the dropdown when I choose current date in the text box using jquery

This is the thing that I am using, please check, if anyone could help me to perform this function with jquery
I have mentioned all the details below of HTML and the jquery function that I have tried
*I have used calendar setup of jquery with the text box to pick the date and for a time I used dropdown, I want time before now disable when I choose the current date. *
This is my date text box
<input name="SetDueDate" type="text" id="SetDueDate" placeholder="Date" onchange="DisableTimeSlots()" style="width:44%;">
This is my time dropdown
<select name="SetTime" id="SetTime" style="width:44%;">
<option value="1|1">09:00:00</option>
<option value="2|1">09:15:00</option>
<option value="3|1">09:30:00</option>
<option value="4|1">09:45:00</option>
<option value="5|1">10:00:00</option>
<option value="6|1">10:15:00</option>
<option value="7|1">10:30:00</option>
<option value="8|1">10:45:00</option>
<option value="9|1">11:00:00</option>
<option value="10|1">11:15:00</option>
<option value="11|1">11:30:00</option>
<option value="12|1">11:45:00</option>
<option value="13|1">12:00:00</option>
<option value="14|1">12:15:00</option>
<option value="15|1">12:30:00</option>
<option value="16|1">12:45:00</option>
<option value="17|1">13:00:00</option>
<option value="18|1">13:15:00</option>
<option value="19|1">13:30:00</option>
<option value="20|1">13:45:00</option>
<option value="21|1">14:00:00</option>
<option value="22|1">14:15:00</option>
<option value="23|1">14:30:00</option>
<option value="24|1">14:45:00</option>
<option value="25|1">15:00:00</option>
<option value="26|1">15:15:00</option>
<option value="27|1">15:30:00</option>
<option value="28|1">15:45:00</option>
<option value="29|1">16:00:00</option>
<option value="30|1">16:15:00</option>
<option value="31|1">16:30:00</option>
<option value="32|1">16:45:00</option>
<option value="33|1">17:00:00</option>
<option value="34|1">17:15:00</option>
<option value="35|1">17:30:00</option>
<option value="36|1">17:45:00</option>
<option value="37|1">18:00:00</option>
<option value="38|1">18:15:00</option>
<option value="39|1">18:30:00</option>
<option value="40|1">18:45:00</option>
<option value="41|1">19:00:00</option>
<option value="42|1">19:15:00</option>
<option value="43|1">19:30:00</option>
<option value="44|1">19:45:00</option>
<option value="45|1">20:00:00</option>
<option value="46|1">20:15:00</option>
<option value="47|1">20:30:00</option>
<option value="48|1">20:45:00</option></select>
This is the calendar I am using with the text box, My function is didn't even called, please suggest how to call this function and how to read the date from the date text box.
$(function () {
CalenderSetup('SetDueDate', 'ViewCalenderDate');
});
function CalenderSetup(InputTxtFieldID, InputButtonID) {
//Please Call its Library on its specific page
Calendar.setup({
inputField: '' + InputTxtFieldID + '', // id of the input
ifFormat: "%m/%d/%Y",
//ifFormat: "%Y-%m-%d", // format of the input field
showsTime: false, // will display a time selector
button: '' + InputButtonID + '', // trigger for the calendar
singleClick: true, // Single-click mode
dateStatusFunc: dateStatus
});
}
I tried this function but not working
function DisableTimeSlots() {
var today = new Date();
if ($("#SetDueDate").text() = today) {
var CurrentTime = new Date($.now());
var a = $("#SetTime").get(0);
for (var i = 0; i < a.length; i++) {
var splitvalue = a[i].value.split('|')[1];
if (a[i] < Now) {
a[i].hidden = true;
}
}
}
}

Reset select dropdown base on hierarchy

<script>
function refresh()
{
document.forms[0].submit();
}
</script>
<select name = "firstoption" onChange ="refresh()"></select>
<option value = "">default option</option>
<option value = "1">1</option>
if($_POST['firstoption]!= "")
<select name = "secondoption" onChange ="refresh()"></select>
<option value = "">default option</option>
<option value = "2">2</option>
if($_POST['secondoption]!= "" && $_POST['firstoption]!= "")
<select name = "thirdoption" onChange ="refresh()"></select>
<option value = "">default option</option>
<option value = "3">3</option>
if($_POST['thirdoption]!= "" && $_POST['secondoption]!= "" && $_POST['firstoption]!= "")
<select name = "fourthoption" onChange ="refresh()"></select>
<option value = "">default option</option>
<option value = "4">4</option>
Hi,
I have 6 static dropdowns. (something like this for some reason I am currently having laptop problems so I could not copy and paste the code where I wrote down generic values)
DELETED (NO LONGER AN ISSUE FIX ON MY OWN)
Basically, I need 6 dropdown (all values kept after it s refresh) and when a dropdown values changes all of the dropdowns below it get reset.
EDIT:
I am looking for code to reset select option back to the default select option (base on hierarchy). Once an select option above it gets change. so if select "2" gets change select 3,4,5 6 value should change to default option. If select "4" gets change select 5,6 would be change to default option etc.
I do not want ajax or jQuery. I am looking for a solution with php, javascript, or html. I think the way to approach it is by comparing the previous and new index number of the select option being change
Note the code I provide is sudo code I can not copy and paste code due to current laptop state.
So the answer does not need to use my code.
I just want a php/javascript/html code that has multiple select options (4-6 select with 2 option in each) the other drop down will be disable until the select above get a value. SO option 2-6 will be disable until select 1 is pick then option 3-6 will be disable until a value for option 2 is pick).
If he user changes select 1 option will select 2-6 already have a value. Select 2-6 automatically switches to default option value. and option 3-6 is now disable until user select option for select 2
Also stack overflow does not allow bounty to be given until 24 hours so I can not give bounty until tomorrow around this time.
Some suggestions to facilitate the solution:
Give all your drop-down lists the same class attribute.
Use just one change event handler on a container element (or the whole document), and let the handler find out which select value was changed.
Create a function that, given an index, will clear all dropdowns from that index onwards, and will disable all of those, except the first one (at that index).
Call this function in the event handler, and also at page load, so to initialise the enabled/disabled status of those dropdowns.
Below is how that could work. I removed all HTML that is not necessary for this solution, but of course you may need more HTML attributes for other purposes:
const dropDowns = Array.from(document.querySelectorAll(".option"));
function reset(i) {
for (let other of dropDowns.slice(i)) {
other.selectedIndex = 0;
other.disabled = other !== dropDowns[i];
}
}
document.addEventListener("change", function (e) {
let i = dropDowns.indexOf(e.target);
if (i < 0) return;
// only allow input in next one if current value is not default:
reset(i+(dropDowns[i].selectedIndex > 0));
});
reset(0); // on page load, disable all except first one
<select class="option">
<option>default option</option>
<option>1</option>
<option>2</option>
</select>
<select class="option">
<option>default option</option>
<option>A</option>
<option>B</option>
</select>
<select class="option">
<option>default option</option>
<option>x</option>
<option>y</option>
</select>
<select class="option">
<option>default option</option>
<option>alpha</option>
<option>beta</option>
</select>
More on the following:
reset(i+(dropDowns[i].selectedIndex > 0));
dropDowns[i].selectedIndex will be 0 when the first entry (default) is selected, and a strictly positive number when any other entry is selected. So with > 0 this actually gives true for when a non-default entry is selected, false otherwise.
Now we want to make the next dropdown available only when the current one has a non-default entry selected, i.e. when this > 0 expression is true. By involving that expression in a + operation, we convert that boolean value to a number (0 for false, 1 for true). And so, that whole expression is either i+0 or i+1, depending on whether the current dropdown has the default value selected or not.
By providing that index (i or i+1) to the reset function, we make sure that the effect of selecting the default value or not is just like is needed.
There are many ways to accomplish this. The below should work with any number of select boxes. The code is commented to explain the steps.
<form accept="#" method="POST" id="myform">
<div>
<select name="firstoption">
<option value="">default option</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
</div>
<div>
<select name="secondoption" disabled>
<option value="">default option</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div>
<div>
<select name="thirdoption" disabled>
<option value="">default option</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</div>
<div>
<select name="fourthoption" disabled>
<option value="">default option</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<div>
<input type="submit" value="Submit">
</div>
</form>
<script>
//get references to the DOM elements we need
var form = document.getElementById('myform');
var selects = form.querySelectorAll('select');
//register event listeners for each of the select boxes so we know when the value changes
for( let i=0; i<selects.length; i++ ) {
selects[i].addEventListener('change', function(evt) {
//select box "i" has changed
//since the value changed, reset other boxes after to the default value and disable
for( let i2 = (i+1); i2 < selects.length; i2++ ) {
selects[i2].value = "";
selects[i2].disabled = true;
}
//if the value of the changed select box is not the default, enable the next one
if( selects[i].value !== "" && selects[i+1] ) {
selects[i+1].disabled = false;
}
});
}
//catch form submission so we can validate the select boxes
form.addEventListener('submit', function(evt) {
//ensure we have all values before submitting
try {
for( let i=0; i<selects.length; i++ ) {
if( selects[i].value === "" ) {
alert(`Please select an option for box number ${i+1}`);
throw 0;
}
}
}catch(e) {
//error, prevent submission
evt.preventDefault();
return false;
}
//all good, submit
return true;
});
</script>
Load all select with document.querySelectorAll() using an expresion to get elements wich name ends with option, that's what $ do before the equal sign.
Then, in your function, check every select if one is empty, set to default value the next.
// Get all items wich name ends with "option"
let selects = document.querySelectorAll('[name$="option"]');
function refresh() {
// Loop into all selects
selects.forEach((item, index) => {
// Don't do this on last one
if(item.value == '' && index < selects.length - 2)
selects[index + 1].value = ""
});
// Check here the sixthoption value if you need it not empty to submit the form
}
<select name="firstoption" onchange="refresh()">
<option value="">Default</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select name="secondoption" onchange="refresh()">
<option value="">Default</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select name="thirdoption" onchange="refresh()">
<option value="">Default</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select name="fourthoption" onchange="refresh()">
<option value="">Default</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select name="fifthoption" onchange="refresh()">
<option value="">Default</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select name="sixthoption" onchange="refresh()">
<option value="">Default</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
Another way using if's:
// Get all items wich name ends with "option"
let selects = document.querySelectorAll('[name$="option"]');
function refresh() {
// 0 = firstoption, 1 = secondoption... 5 = sixthoption
// If value is empty string, set default value for next element too
if(selects[0].value == "") selects[1].value = '';
if(selects[1].value == "") selects[2].value = '';
if(selects[2].value == "") selects[3].value = '';
if(selects[3].value == "") selects[4].value = '';
if(selects[4].value == "") selects[5].value = '';
}
<select name="firstoption" onchange="refresh()">
<option value="">Default</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select name="secondoption" onchange="refresh()">
<option value="">Default</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select name="thirdoption" onchange="refresh()">
<option value="">Default</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select name="fourthoption" onchange="refresh()">
<option value="">Default</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select name="fifthoption" onchange="refresh()">
<option value="">Default</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select name="sixthoption" onchange="refresh()">
<option value="">Default</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
I assume that you not necessarily want to post upon each select change. Instead, let's see this approach:
<select class="fancy-select" id="s-1" name = "firstoption">
<option value = "">default option</option>
<option value = "1">1</option>
</select>
<select class="fancy-select" id="s-2" name = "secondoption">
<option value = "">default option</option>
<option value = "2">2</option>
</select>
<select class="fancy-select" id="s-3" name = "thirdoption">
<option value = "">default option</option>
<option value = "3">3</option>
</select>
<select class="fancy-select" id="s-4" name = "fourthoption">
<option value = "">default option</option>
<option value = "4">4</option>
</select>
<script type="text/javascript">
function myHandler() {
if (!this.value) {
var currentID = parseInt(this.id.substring(2));
for (let i = currentID; i < fancySelects.length; i++) fancySelects[i].value = "";
}
}
var fancySelects = document.querySelectorAll(".fancy-select");
for (let item of fancySelects) item.addEventListener("change", myHandler);
</script>
Fiddle: https://jsfiddle.net/0dfxytpc/1/
You see that despite the claims about Vue JS and jQuery making it easier, Javascript in itself is easy. If you know how to program in Javascript. My understanding is that once default is chosen you intend the subsequent items to be defaulted. If that's not the aim, then I misunderstood the goal.
Now, you can notice that this is pretty repetitive, we can surely make it nicer to appear. Let's generate the select tags on the server:
<?php
for ($index = 1; $index <= 4; $index++) {
?>
<select class="fancy-select" id="s-<?php echo $index; ?>">
<option value="">default option</option>
<option value="<?php echo $index; ?>"><?php echo $index; ?></option>
</select>
<?php
}
?>
<script type="text/javascript">
function myHandler() {
if (!this.value) {
var currentID = parseInt(this.id.substring(2));
for (let i = currentID; i < fancySelects.length; i++) fancySelects[i].value = "";
}
}
var fancySelects = document.querySelectorAll(".fancy-select");
for (let item of fancySelects) item.addEventListener("change", myHandler);
</script>
The code is small and easy to understand. Vue JS and jQuery would just overcomplicate this, they would add dependencies that you do not need. There is a trend of programmers who are less capable to work in Javascript and will argue that doing stuff is easier in Vue JS or jQuery. You can actually measure the power of that trend with the number of downvotes I get. I do not speak against the use of jQuery or Vue JS in general, although, I am not their fan in particular, but when you learn Javascript you should avoid getting dependant of a framework right from the start. As you get comfortable working with Javascript you might decide that Vue JS and jQuery is good for you. I would totally respect that decision, but do not make that decision before you learn Javascript properly.
You could try this:
const selects = document.querySelectorAll(".myoption");
const submit = document.getElementById("mysubmit");
selects.forEach((select, id) => select.addEventListener("change",() => {
const last = id === selects.length - 1;
if(! last) {
selects[id + 1].removeAttribute("disabled");
selects[id + 1].value = "";
}
for(let i = id + (select.selectedIndex ? 2 : 1); i < selects.length; ++i) {
selects[i].value = "";
selects[i].setAttribute("disabled", true);
}
submit.setAttribute("disabled", true);
if(last && select.selectedIndex) submit.removeAttribute("disabled");
}));
<select class="myoption">
<option value="">Default</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<select class="myoption" disabled>
<option value="">Default</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<select class="myoption" disabled>
<option value="">Default</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<select class="myoption" disabled>
<option value="">Default</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<select class="myoption" disabled>
<option value="">Default</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<select class="myoption" disabled>
<option value="">Default</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<input type="submit" id="mysubmit" value="submit" disabled>
Hope this helps.
As already a lot of ways are mentioned, here is what I came up with.
Assign every select a data-name attribute.
Every select would also have a class attribute. This class attribute would possess all previous parents' data-names.
When a particular select value changes, you get the data-name of it and use querySelectorAll to get all those elements who have this data-name in their class and set their values and disability accordingly.
var selects = document.querySelectorAll('select');
selects.forEach(function(select_dropdown){
select_dropdown.addEventListener('change',function(){
var kids = document.querySelectorAll('.' + this.getAttribute('data-name'));
var parent_value = this.value;
kids.forEach(function(child,index){
child.value = "";
if(parent_value == "" || index > 0){
child.setAttribute('disabled','disabled');
}else{
child.removeAttribute('disabled');
}
});
});
});
<select class="" data-name="select_1">
<option value="">Default</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<select data-name="select_2" class="select_1" disabled>
<option value="">Default</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<select data-name="select_3" class="select_1 select_2" disabled>
<option value="">Default</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<select data-name="select_4" class="select_1 select_2 select_3" disabled>
<option value="">Default</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<select data-name="select_5" class="select_1 select_2 select_3 select_4" disabled>
<option value="">Default</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
<select data-name="select_6" class="select_1 select_2 select_3 select_4 select_5" disabled>
<option value="">Default</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
You can add data attribute for each dropdown, that way you can track index of that dropdown, so on change all higher than index apply action..
when option is selected, reset rest of the boxes that are in order.
If previus select box not default, enable next box else disable it
var selects = document.getElementsByClassName("select")
for(var i = 0; i < selects.length; i++){
selects[i].addEventListener('change', function(e){
for(var x = 0; x < selects.length; x++){
//On select, reset selectboxes that commes after
if(Number(e.target.dataset.columns) < x){
selects[x][0].selected = true;
}
//If previus select box not default, enable next box
if(x > 0 && selects[x -1][0].selected != true){
selects[x].disabled = false;
}
//else dissable next box..
else if(x > 0 && selects[x -1][0].selected == true){
selects[x].disabled= true;
}
}
})
}
<select data-columns="0" class="select">
<option value = "">default option</option>
<option value = "1">1</option>
<option value = "1">1</option>
</select>
<select data-columns="1" disabled class="select">
<option value = "">default option</option>
<option value = "2">2</option>
<option value = "1">1</option>
</select>
<select data-columns="2" disabled class="select">
<option value = "">default option</option>
<option value = "3">3</option>
<option value = "1">1</option>
</select>
<select data-columns="3" disabled class="select">
<option value = "">default option</option>
<option value = "4">4</option>
<option value = "1">1</option>
</select>

Javascript take number selected in a drop down and times by 1.09?

I need to create a drop down menu containing numbers,
<select>
<option value="32">32</option>
<option value="33">33</option>
<option value="34">34</option>
<option value="35">35</option>
</select>
and when a number is selected I want the value to be returned in a <p id="returned-value"></p>tag, but times by 1.09. How can I do that with javascript?
See this fiddle
As you can see in the fiddle, the (selected value * 1.9) is shown in the <p> as soon as a value is selected from the <select>. This is done using onchange. The onchange event occurs when the value of an element has been changed.
HTML
<select id="mySelect" onchange="myFunction()">
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<p id="demo">0</p>
JS
function myFunction() {
var x = document.getElementById("mySelect").value;
document.getElementById("demo").innerHTML = "You selected: " + x * 1.9;
}
var value = parseInt(document.getElementByTagName('select').value);
var newValue = value * 1.09;
document.getElementById('returned-value').innerHTML = newValue;

Setting value to a form field with JavaScript

I have a web form with several fields. Three of them are ano_evento, mes_evento and dia_evento (year, month and day). I want to get the values from these fields, convert it to a date format and show the formatted date in another field called fecha_evento. The user sets a value for ano_evento (e.g. 2014), a value for mes_evento (e.g. 11) and a value for dia_evento (e.g. 25). The value for fecha_evento should then be 2014-11-25.
All three fields have an onchange method in it:
For ano_evento:
<select name="ano_evento" id ="ano_evento" onchange="cambiar_fecha()">
For mes_evento:
<select name="mes_evento" id ="mes_evento" onchange="cambiar_fecha()">
For dia_evento:
<select name="dia_evento" id="dia_evento" onchange ="cambiar_fecha()">
The field for fecha_evento:
<input type="text" name="fecha_evento" id="fecha_evento" value="0" size="32" />
And this is the script:
<script>
function cambiar_fecha(){
alert (document.getElementById("fecha_evento").value);
var ano = document.getElementById("ano_evento").value;
var mes = document.getElementById("mes_evento").value; //
var dia = document.getElementById("dia_evento").value;
document.getElementById("fecha_evento").value = ano+"-"+mes+"-"+dia;
}
</script>
When the user changes any of the three fields values, the alert inside the script is shown, but the value from the field fecha_evento doesn't change.
Any help is welcome
how are you?
Try this code:
<select name="ano_evento" id="ano_evento">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select name="mes_evento" id="mes_evento">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select name="dia_evento" id="dia_evento">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="text" name="fecha_evento" id="fecha_evento">
<script>
var ano = document.getElementById("ano_evento");
var mes = document.getElementById("mes_evento");
var dia = document.getElementById("dia_evento");
var fecha = document.getElementById("fecha_evento");
function cambiar_fecha() {
alert(fecha.value);
fecha.value = ano.value + "-" + mes.value + "-" + dia.value;
alert(fecha.value);
}
ano.onchange = cambiar_fecha;
mes.onchange = cambiar_fecha;
dia.onchange = cambiar_fecha;
</script>
Recommendations:
- Save the elements in variables to optimize execution avoiding constantly DOM access.
- Set events handler from JS.
- Use browser console to see if there are JS errors.
Regards.
Instead of the inline javascript, I would just go with a querySelectorAll
DEMO
HTML:
<select name="ano_evento" id ="ano_evento" >
<option value="1">1</option>
<option value="2">2</option>
</select>
<select name="mes_evento" id ="mes_evento" >
<option value="1">1</option>
<option value="2">2</option>
</select>
<select name="dia_evento" id="dia_evento" >
<option value="1">1</option>
<option value="2">2</option>
</select>
<input type="text" name="fecha_evento" id="fecha_evento" value="0" size="32" />
Javascript:
var a = document.querySelectorAll('select');
console.log(a);
for(var i = 0; i<a.length;i++) {
a[i].addEventListener('change',cambiar_fecha);
}
function cambiar_fecha(){
alert (document.getElementById("fecha_evento").value);
var ano = document.getElementById("ano_evento").value;
var mes = document.getElementById("mes_evento").value; //
var dia = document.getElementById("dia_evento").value;
document.getElementById("fecha_evento").value = ano+"-"+mes+"-"+dia;
}

Not able to get value from the dynamically generated drop down

Problem
I've 2 diff drop down select box. First select box is static whereas the other is dynamically generated according to the value selected in first select box. The values are getting generated properly but when I try to display the value of second select box it only display the first option's value, even if selected 3rd or 4th option. Please see code and example below:
Javascript Code
function wpPopulate(){
$('#win_parameter').empty();
var classes = document.getElementById('noOfClasses').value;//total number of classes
var percent = (100/classes);//to get a percentage for single class
//console.log("1 class: "+percent+"%");
//Define Variables
var wpOptions = '';
var dd = '';
for(var i=1; i<=classes; i++){
dd = parseFloat(percent*i).toFixed(2)+"% ("+i+" class)";
wpOptions += '"'+parseFloat(percent*i).toFixed(2)+'": "'+dd+'", ';
}
wpOptions = "{"+wpOptions.substr(0, (wpOptions.length-2))+"}";
wpOptions = $.parseJSON(wpOptions);
var selectValues = wpOptions;
$.each(selectValues, function(key, value) {
$('#win_parameter')
.append($("<option></option>")
.attr("value",key)
.text(value));
});
}
HTML Code:
(Select box 1)
<select name="noOfClasses" id="noOfClasses" onChange="wpPopulate();">
<option value="1" selected="selected">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>
<option value="11">11</option>
<option value="12">12</option>
</select>
(Select box 2)
<select name="win_parameter" id="win_parameter">
</select>
Example:
When selected 5 classes in first select box the options come in second select box are as follows. When I select anything apart from 20% it still displays as 20%, example 60%. Any help or guidance will be much appreciated!!
<select name="win_parameter" id="win_parameter">
<option value="20.00">20.00% (1 class)</option>
<option value="40.00">40.00% (2 class)</option>
<option value="60.00">60.00% (3 class)</option>
<option value="80.00">80.00% (4 class)</option>
<option value="100.00">100.00% (5 class)</option>
</select>

Categories

Resources