This works, does anyone see anything I shouldn't be doing?
My function that is called
function getWeight(){
var weight;
var quantity = document.dhform.quantity.value;
var cardSize = document.dhform.cardSize.value;
weight = quantity * cardSize;
document.rates.weight.value = weight;
}
takes values from these drop down menues
<td><span class="style29">Quantity</span><span class="style1"><br/>
<select id="Quantity" name="quantity" size="1">
<option value="250">250</option>
<option value="500">500</option>
<option value="1000" selected>1000</option>
<option value="1500">1500</option>
<option value="2000">2000</option>
<option value="2500">2500</option>
<option value="3000">3000</option>
<option value="4000">4000</option>
<option value="5000">5000</option>
</select>
</span></td>
<td><p><span class="style1"><span class="style29">Size</span><br/>
<select id="Size" name="Size" size="1" onChange="getWeight()">
<option value="0.00999" selected>8.5 x 3.5</option>
<option value="0.0146">11 x 4</option>
</select>
</span></p></td>
Value needs to be inserted into this text box
<td style="width: 115px; height: 49px;"><span class="style16">Weight</span><br/>
<input type="text" id="weight" name="weight" size="10" maxlength="4"/>
</td>
Yes, it's done with Javascript. Let's say the "choose something" part is a drop-down (an HTML select box). You add an "onchange" event handler to that select box which fires a javascript function (which will automatically get the changed select box element as a parameter). Within that function, you use the value of the select box to determine what you want the value of the other box to be, and you update that other box's value.
Example:
<head>
<script language="JavaScript">
function setToy(dropDown) {
var pet = dropDown.options[dropDown.selectedIndex].value);
var newBox = document.getElementById("toy");
var toyText = "";
switch(pet) {
case "dog": toyText = "bone";
case "cat": toyText = "mouse";
default:toyText = "";
}
newBox.innerHTML = toyText;
}
</script></head>
<body>
<select name="petDropDown" onChange="updateToy(this)">
<option value="dog">Dog</option>
<option value="cat">Cat</option>
</select><br />
Preferred Toy: <input id="toy" />
</body>
I'll add that if you do this stuff a lot, you should look into jQuery, which makes this kind of thing much easier.
Related
I am very new to javascript. So i have a select option and an input field. What i want to achieve is to have the value of the input field change when i select a particular option. This is what i have tried:
First Name: <input type="text" value="colors">
<select name="">
<option>Choose Database Type</option>
<option onclick="myFunction(g)>green</option>
<option onclick="myFunction(r)>red</option>
<option onclick="myFunction(o)>orange</option>
<option onclick="myFunction(b)>black</option>
</select>
<script>
function myFunction(g) {
document.getElementById("myText").value = "green";
}
function myFunction(r) {
document.getElementById("myText").value = "red";
}
function myFunction(o) {
document.getElementById("myText").value = "orange";
}
function myFunction(b) {
document.getElementById("myText").value = "black";
}
</script>
A few things:
You should use the onchange function, rather than onclick on each individual option.
Use a value attribute on each option to store the data, and use an instance of this to assign the change (or event.target)
You have no ID on your text field
You're missing the end quote for your onclick function
<select name="" onchange="myFunction(event)">
<option disabled selected>Choose Database Type</option>
<option value="Green">green</option>
<option value="Red">red</option>
<option value="Orange">orange</option>
<option value="Black">black</option>
</select>
And the function:
function myFunction(e) {
document.getElementById("myText").value = e.target.value
}
And add the ID
<input id="myText" type="text" value="colors">
Fiddle: https://jsfiddle.net/gasjv4hs/
More proper way is to put your JS code in a different .js file and use Jquery as when you go further with your programming this is the proper way.
Your HTML
<input type="text" id="color" name="color">
<select name="" id="changeData">
<option>Choose Database Type</option>
<option data-value="green">green</option>
<option data-value="red">red</option>
<option data-value="orange">orange</option>
<option data-value="black">black</option>
</select>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
Your JS
$(document).ready(function () {
$('#changeData').change(function(){
var color = $(this).val();
$('#color').val(color);
})
});
Also make sure that you have added Jquery Library to your Project. You can either download Jquery and add in your project folder OR also you can use CDN. in this example CDN is used.
I came up with a similar problem. In my case i was trying to change the minimum value of an input based on the value of an option in a select list. I tried to apply the above solutions but nothing worked. So i came with this, which can be applied to problems similar to this
HTML
<input id="colors" type="text" value="">
<select id="select-colors" name="" onchange="myFunction()">
<option disabled selected>Choose Colour</option>
<option value="green">green</option>
<option value="red">red</option>
<option value="orange">orange</option>
<option value="black">black</option>
</select>
JS
function myFunction() {
var x = document.getElementById("select-colors").value;
document.getElementById("colors").value = x;
}
This works by getting the value of the select with id "select-colors" on every change, assigning it into a variable "x" and inserting it into the input value with id "colors". This can be implemented in anyway based on your problem
You create functions with same name multiple times.
Only last one will work.
the variables you pass g,r,o,b are undefined.
Don't add onclick to option add onchange to select.
Make use of HTML5 data-* attribute
function myFunction(element) {
document.getElementById("myText").value = element;
}
First Name: <input type="text" id="myText" value="colors">
<select name="" onchange="myFunction(this.value);">
<option>Choose Database Type</option>
<option data-value="green">green</option>
<option data-value="red">red</option>
<option data-value="orange">orange</option>
<option data-value="black">black</option>
</select>
You can resolve it this way.
` First Name:
<select name="" onchange="myFunction()" id="selectID">
<option>Choose Database Type</option>
<option >green</option>
<option >red</option>
<option >orange</option>
<option >black</option>
</select>
<script>
function myFunction() {
var selectItem = document.getElementByID('selectID').value;
document.getElementByID('yourId').value = sekectItem;
}
</script>
Here's a way to achieve that:
var select = document.getElementById('colorName');
function myFunction(event) {
var color = 'No color selected';
if (select.selectedIndex > 0) {
color = select.item(select.selectedIndex).textContent;
}
document.getElementById('myText').value = color;
}
select.addEventListener('click', myFunction);
myFunction();
First Name: <input type="text" id="myText">
<select id="colorName">
<option>Choose Database Type</option>
<option>green</option>
<option>red</option>
<option>orange</option>
<option>black</option>
</select>
Your code should be like following.
<input type="text" name="color" id="color">
<select name="" id="change_color">
<option>Choose Database Type</option>
<option >green</option>
<option >red</option>
<option >orange</option>
<option >black</option>
</select>
<script type="text/javascript" src="jquery.js"></script>
<script>
$(document).ready(function () {
$('#change_color').change(function(){
var color = ($(this).val());
$('#color').val(color);
})
});
</script>
Here is JS Code that worked for me
var selectYear = document.getElementById('select-year');
selectYear.addEventListener('change', function() {
var selectedYear = document.getElementById('selected-year');
selectedYear.innerHTML = selectYear.value;
});
Select Input ID: select-year
Text ID: selected-year
Code generated from Codex AI :)
<span id="s_home_country">
<select name="Home Country" id="home_country">
<option value="Select one" selected="selected">Select one</option>
<option value="h1">Home1</option>
<option value="h2">Home2</option>
<option value="h3">Home3</option>
<option value="h4">Home4</option>
</select>
</span>
and I have another data that i want to incorporate, when Home1 is selected, I want this to show 21-dec into another textbox automatically. and so on..
var Home_country = [
"21-Dec",
"01-Jan",
"01-Jan",
"01-Jan",
];
You could use an object and an event listener.
var home_country = {
"h1":"21-Dec",
"h2":"01-Jan",
"h3":"01-Jan",
"h4":"01-Jan",
};
var selectbox = document.getElementById('home_country');
var textbox = document.getElementById('home_country_date');
selectbox.addEventListener('change', function(e){
textbox.value = home_country[this.value]
})
<span id="s_home_country">
<select name="Home Country" id="home_country">
<option value="Select one" selected="selected">Select one</option>
<option value="h1">Home1</option>
<option value="h2">Home2</option>
<option value="h3">Home3</option>
<option value="h4">Home4</option>
</select>
</span>
<input type="text" id="home_country_date">
Please find below snippet just add another attribute data-attr1 and on change of select list find selected option and get its attribute with name data-attr1 and pass attribute's value to textbox
$("#home_country").on("change",function(){
$(".txtvalue").val($(this).find("option:selected").attr("data-attr1"));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="s_home_country">
<select name="Home Country" id="home_country">
<option value="Select one" selected="selected">Select one</option>
<option data-attr1="21-Dec" value="h1">Home1</option>
<option data-attr1="01-Dec" value="h2">Home2</option>
<option data-attr1="02-Dec" value="h3">Home3</option>
<option data-attr1="03-Dec" value="h4">Home4</option>
</select>
</span>
<input type="text" class="txtvalue" />
Add an event listener to your select input which outputs the relevant value from your array when an option is selected:
var el = document.getElementById('home_country');
var textbox = document.getElementById('your_textbox_el');
el.addEventListener('change', function(e){
if (e.target.value == 'h1') {
textbox.value = Home_country[0];
} else if (e.target.value == 'some other condition') {
// do something else
}
}
Unless I'm misunderstanding you, you can just put the values of the Home_country array inside the value properties of the options like so:
<span id="s_home_country">
<select name="Home Country" id="home_country">
<option value="Select one" selected="selected">Select one</option>
<option value="21-Dec">Home1</option>
<option value="01-Jan">Home2</option>
<option value="01-Jan">Home3</option>
<option value="01-Jan">Home4</option>
</select>
</span>
Now, when you do get the value of your select element, it will be the value in the array that you gave.
I have these conditional drop lists behaving on screen as expected, but I cannot get the selected values from the drop downs to output in the HTML form (I can if I don't include the javascript). Only the text inputs are outputing as per the xml result below (Company & Add1). I want the xml to contain the Location from the first drop down, and the selected city from the conditional 2nd drop down.
<body>
<form action="http://TESTPLANETPRESS:8080/ObtainQuote" method="GET" >
<fieldset>
<legend>Location</legend>
<select id="country" class="source" onchange="updateSelectTarget()">
<option value="England">England</option>
<option value="France">France</option>
<option value="Germany">Germany</option>
</select>
<select id="England">
<option value="Birmingham">Birmingham</option>
<option value="Liverpool">Liverpool</option>
<option value="London">London</option>
</select>
<select id="France" class="hidden">
<option value="Lyon">Lyon</option>
<option value="Marseille">Marseille</option>
<option value="Paris">Paris</option>
</select>
<select id="Germany" class="hidden">
<option value="Berlin">Berlin</option>
<option value="Hamburg">Hamburg</option>
<option value="Munich">Munich</option>
</select>
<label for="Company">Company:</label><input type="text" name="Company" value="Google">
<label for="Add1">Add1:</label><input type="text" name="Add1" value="1 Nowhere Street">
</fieldset>
<input type="submit" value="Submit">
</form>
<script>
function updateSelectTarget () {
var id = this.options[this.selectedIndex].value;
var targets = this.parentNode.getElementsByTagName("select");
var len = targets.length;
for (var i = len - 1; i > 0; --i) {
if (targets[i].id == id) {
targets[i].style.display = "block";
}
else {
targets[i].style.display = "none";
}
}
}
function initChangeHandler () {
var el = document.getElementById("country");
el.onchange = updateSelectTarget;
el.onchange();
}
window.onload = initChangeHandler;
</script>
</body>
Current XML result, (Does not include the results from the two drop downs).
<?xml version="1.0"?>
-<request type="GET">
<paths count="0"/>
-<values count="2">
<Company>Google</Company>
<Add1>1 Nowhere Street</Add1>
</values>
Do you want the value attribute or the text? Based on Get selected value in dropdown list using JavaScript? (similar to the first part), .value should work for the value attribute and .text for the text that is selected.
Also, please make two different questions instead of one question with 2 questions nested inside.
Please forgive this novice user! I am trying to create a table in which the user would select from a list of concert programs and then select a date (from among 2 or 3 associated dates) for the program selected. The same set of pulldown menus must be repeated 3 additional times. This is for a partial season subscription package in which a patron can select 4 out of 6 programs and the desired date for each program, and all that information needs to post to Paypal for processing/purchasing.
Please see this test page: http://early-music.org/Test_forTzN.html and this jsfiddle.net page: jsfiddle.net/saraswati/v6Pur/31
As one can see on the webpage, my script works for "Concert 1," but not for Concerts 2, 3, and 4. On the jsfiddle page, even Concert 1 doesn't work.
Thank you for your help!
Here is the code:
<script type="text/javascript">
var progamsAndDates = {};
progamsAndDates['TUDORS'] = ['Sept. 15', 'Sept. 16'];
progamsAndDates['NOCHES'] = ['Oct. 20', 'Oct. 21'];
progamsAndDates['CHRISTMAS'] = ['Dec. 14', 'Dec. 15', 'Dec. 16'];
progamsAndDates['CELTIC'] = ['Jan. 26', 'Jan. 27'];
progamsAndDates['UNREQUITED'] = ['Mar. 02', 'Mar. 03'];
progamsAndDates['SECRET'] = ['Apr. 20', 'Apr. 21'];
function ChangeDateDropList (id) {
var programDropList = document.getElementById ("program"+id);
var dateDropList = document.getElementById ("date"+id);
var selProgram = programDropList.options[programDropList.selectedIndex].value;
// remove all dates
while (dateDropList.options.length) {
dateDropList.remove (0);
}
// add new dates
var dates = progamsAndDates[selProgram];
if (dates) {
for (var i=0; i < dates.length; i++) {
var date = new Option (dates[i], i);
dateDropList.options.add (date);
}
}
}
</script>
<tr id=row2>
<td><input type="hidden" name="os0" value="Concert 1"><body onload="ChangeDateDropList ();">
<select id="program" onchange="ChangeDateDropList();">
<option value="" selected="selected">Select a Program</option>
<option value="TUDORS">The Tudors</option>
<option value="NOCHES">Noches, Noches</option>
<option value="CHRISTMAS">Christmas Eurotour</option>
<option value="CELTIC">Celtic Trinity</option>
<option value="UNREQUITED">Unrequited Love</option>
<option value="SECRET">Secret No More</option>
</select>
<select id="date">
</select>
</body>
</td>
<td><input type="hidden" name="os1" value="Concert 2"><body onload="ChangeDateDropList ();">
<select id="program1" onchange="ChangeDateDropList(1);">
<option value="" selected="selected">Select a Program</option>
<option value="TUDORS">The Tudors</option>
<option value="NOCHES">Noches, Noches</option>
<option value="CHRISTMAS">Christmas Eurotour</option>
<option value="CELTIC">Celtic Trinity</option>
<option value="UNREQUITED">Unrequited Love</option>
<option value="SECRET">Secret No More</option>
</select>
<select id="date1">
</select>
</body>
</td>
<td><input type="hidden" name="os2" value="Concert 3"><body onload="ChangeDateDropList ();">
<select id="program2" onchange="ChangeDateDropList(2);">
<option value="" selected="selected">Select a Program</option>
<option value="TUDORS">The Tudors</option>
<option value="NOCHES">Noches, Noches</option>
<option value="CHRISTMAS">Christmas Eurotour</option>
<option value="CELTIC">Celtic Trinity</option>
<option value="UNREQUITED">Unrequited Love</option>
<option value="SECRET">Secret No More</option>
</select>
<select id="date2">
</select>
</body>
</td>
<td><input type="hidden" name="os3" value="Concert 4"><body onload="ChangeDateDropList ();">
<select id="program3" onchange="ChangeDateDropList(3);">
<option value="" selected="selected">Select a Program</option>
<option value="TUDORS">The Tudors</option>
<option value="NOCHES">Noches, Noches</option>
<option value="CHRISTMAS">Christmas Eurotour</option>
<option value="CELTIC">Celtic Trinity</option>
<option value="UNREQUITED">Unrequited Love</option>
<option value="SECRET">Secret No More</option>
</select>
<select id="date3">
</select>
</body>
</td>
</tr>
</table>
<br>
<center>
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_cart_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
</center>
You have a couple of issues:
You have multiple "program" and "date" elements. Each one of those needs its own unique ID so that you know which one you're referring to in your javascript.
Your ChangeDateDropList function expects 1 argument and you're not passing any. (Note: This isn't incorrect syntax. Just pointing it out since it appears were going down the correct route needing to pass an argument to your function but forgot to actually pass the argument in your function calls).
The simplest way to fix this is to rename each program and date element with a numerical suffix, ex: "program1", "date1", "program2", "date2", etc. Once that's done you can pass the numerical suffix in to the ChangeDateDropList function and then use that to refer to the elements correctly:
function ChangeDateDropList (id) {
var programDropList = document.getElementById ("program"+id);
var dateDropList = document.getElementById ("date"+id);
...
Don't forget to update your onchange calls also:
<select id="program1" onchange="ChangeDateDropList(1);">
I have a drop down menu, with two options:
<select name="chs" value="" id = ""/>
<option value="">ICE Cream</option>
<option value="">Juice</option>
</select>
This is what I want to have: in the first page you see a select, with two option, if you choose ICE cream another select will appear with different flavors (chocolate, vanilla, etc), and the same for juice.
This is what I already have:
html:
<label>Choose Skin</label><br /><select name="na" value="" id = ""/>
<option value="">ICE Cream</option>
<option value="">Juice</option>
</select>
<select name="icecreamfla" value="" onchange="showSelect()" id = "framework"/>
<option id="jm">vanilla</option>
<option id = "wp">chocholate</option>
</select>
<input type="submit" name = "redirect" value="go" />
The JavaScript:
function showSelect()
{
var select = document.getElementById('skin');
if(select.value == "juice"){
alert('saeed khare!');
}
else{
var option = document.getElementById('icecreamfla');
option.style.display = 'inline';
}
}
option.style.display used to be 'none', then I changed it to inline.
if any one can help please do it.
Here is a small demo with the flavours <select> only appearing when Ice Cream is selected.
It should be easy to follow to implement this to display a different <select> if the user chooses Juice.
Edit: If you want the selected value you can use types.options[types.selectedIndex].text to get it using the variables in my demo.
Is this you want to do it .
<script language='javascript'>
function showSelect()
{
var select = document.getElementById('skin').value;
if(select == "ICE Cream"){
var str="<select name='icecreamfla' value='' id = 'icecreamfla'/>"
+"<option id='jm'>vanilla</option>"+
"<option id ='wp'>chocholate</option> </select>";
document.getElementById("flavour").innerHTML=str;
}
}
<label>Choose Skin</label><br /><select name="na" value="" id = "skin" onchange="showSelect()">
<option value="ICE Cream">ICE Cream</option>
<option value="Juice">Juice</option>
</select>
<div id="flavour"></div>
<input type="submit" name = "redirect" value="go" />