I'd like to have a dynamic result as consequence of a drop-down choice in my website. I have the following example with cars. Buying a volvo should result in car costs ar 1000 for example. I'd like to have this real time, depending on the dropdown choice that is made.
Could you help me with this? I think it must be some kinde of javascript, but I can't figure out how to do this.
<!DOCTYPE html>
<html>
<body>
<style>
#choice {
float: right;
}
</style>
<form action="action_page.php">
<select name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
<br><br>
<input type="submit">
</form>
<div id="choice">
Car costs are: Result of choice
</div>
</body>
</html>
Thank you!
This should work just fine.
<html ng-app>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script>
function Car($scope) {
$scope.car = {
'Volvo': {
'Volvo-2000': ['Volvo-2000'],
'Volvo-3000': ['Volvo-3000']
},
'Saab': {
'Saab-2000': ['Saab-2000'],
'Saab-3000': ['Saab-3000']
},
'Fiat': {
'Fiat-2000': ['Fiat-2000'],
'Fiat-3000': ['Fiat-3000']
},
'Audi': {
'AUDI-2000': ['AUDI-2000'],
'AUDI-3000': ['AUDI-3000']
}
};
}
</script>
</head>
<body>
<div ng-controller="Car">
<div>
Car:
<select id="car" ng-model="price" ng-options="car for (car, price) in car">
<option value=''>Select</option>
</select>
</div>
<div>
Price: <select id="price" ng-disabled="!car" ng-model="price" ng-options="car for (car,price) in price"><option value=''>Select</option></select>
</div>
</div>
</body>
</html>
Fiddle
EDIT:
Sorry for the misconception, this should be just fine.
<!DOCTYPE html>
<html>
<body>
<style>
#choice{ float:right;}
</style>
<script>
var changeInput = function (val){
var input = document.getElementById("choice");
input.value = val;
}
</script>
<form action="action_page.php">
<select name="cars" onchange = 'changeInput(this.value);'>
<option value="volvo 121">Volvo</option>
<option value="saab 121">Saab</option>
<option value="fiat 121">Fiat</option>
<option value="audi 121">Audi</option>
</select>
<br><br>
<input type="submit">
</form>
<div>
Car cost:
<form>
<input type = "text" id = "choice" value = "" style="float: left;">
</form>
</div>
</body>
</html>
2nd Fiddle
Related
How about, I have a <select> tag which is filled with the BD.
What I want to do is that when selecting the desired option, that the <select> values are assigned to different <input> tags.
Currently I select some of the options, I get the value and the text of the option. I want to have them as 2 different options.
In my example I have this <select>
<option value = "1"> Pizza1 Pizza2 </ option>
My question is how to separate the text of the option so that Pizza 1 is in one option and Pizza2 is in a different option.
So that something like this:
Code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form action="#">
<select name="choose_food" onchange="showValue(this.value); showValue2(this.options[this.selectedIndex].innerHTML);">
<option value="1">Pizza1 Pizza2</option>
<option value="2">Hamburger1 Hamburger2</option>
<option value="3">Bacon1 Bacon2</option>
</select>
<br/>
<input type="text" name="food" id="food" value="" />
<br/>
<input type="text" name="food2" id="food2" value="" />
</form
</body>
</html>
<script type="text/javascript">
var showValue = function(x){
document.getElementById('food').value=x;
}
var showValue2 = function(x){
document.getElementById('food2').value=x;
}
</script>
Just put them in different option values as follows:
<option value="1">Pizza1</option>
<option value="2">Pizza2</option>
I hope I got what you mean. To make them different options, you just have to put them into different option tags, like so:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form action="#">
<select name="choose_food" onchange="showValue(this.value); showValue2(this.options[this.selectedIndex].innerHTML);">
<option value="1">Pizza1</option>
<option value="2">Pizza2</option>
<option value="3">Hamburger1 Hamburger2</option>
<option value="4">Bacon1 Bacon2</option>
</select>
<br/>
<input type="text" name="food" id="food" value="" />
<br/>
<input type="text" name="food2" id="food2" value="" />
</form
</body>
</html>
<script type="text/javascript">
var showValue = function(x){
document.getElementById('food').value=x;
}
var showValue2 = function(x){
document.getElementById('food2').value=x;
}
</script>
I tried to find a solution with the stackoverflow search but I did not understand what I found.
Basically I want to have a List from which I can choose a value, which changes a link from the button. I think I need Javascript for it, so I want to ask you, how the code would look like?
EDIT:
So basically i want choose one option of the select, and every option has an own link to another html page. If i click on a button the html page of the chosen option opens. How do I do it with Java Script?
<form action="select.html">
<select name="Bundesländer" size="16">
<option selected>Baden-Würtemberg</option>
<option>Bayern</option>
<option>Berlin</option>
<option>Brandenburg</option>
<option>Bremen</option>
<option>Hamburg</option>
<option>Hessen</option>
<option>Mecklenburg Vorpoomern</option>
<option>Niedersachsen</option>
<option>NRW</option>
<option>Rheinland</option>
<option>Saarland</option>
<option>Sachsen</option>
<option>Sachsen-Anhalt</option>
<option>Schleswig-Holstein</option>
<option>Thüringen</option>
</select>
</form>
Just check it. Hope this is what you want.
function selectFunction() {
var x = document.getElementById("selectopt").value;
document.getElementById("mylink").innerHTML = x;
document.getElementById("mylink").href = "http://www." + x + ".com";
}
<form action="select.html">
<select id="selectopt" onchange="selectFunction(this);" name="Bundesländer" size="16">
<option selected>Baden-Würtemberg</option>
<option>Bayern</option>
<option>Berlin</option>
<option>Brandenburg</option>
<option>Bremen</option>
<option>Hamburg</option>
<option>Hessen</option>
<option>Mecklenburg Vorpoomern</option>
<option>Niedersachsen</option>
<option>NRW</option>
<option>Rheinland</option>
<option>Saarland</option>
<option>Sachsen</option>
<option>Sachsen-Anhalt</option>
<option>Schleswig-Holstein</option>
<option>Thüringen</option>
</select>
</form>
<a id="mylink" style="position:absolute;top:0;right:0;" href="http://google.com">Link</a>
You can use this code:
<script type="text/javascript">
function redirect(select) {
window.location = select.options[select.selectedIndex].getAttribute('data-link');
}
</script>
<form action="select.html">
<select name="Bundesländer" size="16" onChange="redirect(this)">
<option data-link="http://www.google.fr/" selected>Baden-Würtemberg</option>
<option data-link="http://www.google.com/">Bayern</option>
<option data-link="http://www.google.de/">Berlin</option>
<option data-link="http://www.google.it/">Brandenburg</option>
<option data-link="http://www.google.be/">Bremen</option>
<option data-link="http://www.google.be/">Hamburg</option>
<option data-link="http://www.google.be/">Hessen</option>
<option data-link="http://www.google.be/">Mecklenburg Vorpoomern</option>
<option data-link="http://www.google.be/">Niedersachsen</option>
<option data-link="http://www.google.be/">NRW</option>
<option data-link="http://www.google.be/">Rheinland</option>
<option data-link="http://www.google.be/">Saarland</option>
<option data-link="http://www.google.be/">Sachsen</option>
<option data-link="http://www.google.be/">Sachsen-Anhalt</option>
<option data-link="http://www.google.be/">Schleswig-Holstein</option>
<option data-link="http://www.google.be/">Thüringen</option>
</select>
</form>
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script>
$(document).ready(function () {
$('select').change(function () {
$('#btn').attr('url_value', $(this).val());
});
$('#btn').click(function () {
window.open($(this).attr('url_value'));
// alert($(this).attr('url_value'))
});
});
</script>
<body>
<form >
<input type="button" value="Open Selected" id="btn" url_value=""/>
<select name="Bundes" >
<option selected>Baden-</option>
<option>http://www.w3schools.com</option>
<option>http://www.gmail.com</option>
<option>Brandenburg</option>
<option>Bremen</option>
<option>Hamburg</option>
<option>Hessen</option>
<option>Mecklenburg </option>
<option>Niedersachsen</option>
<option>NRW</option>
<option>test</option>
<option>Saarland</option>
<option>Sachsen</option>
<option>Sachsen-</option>
<option>Schleswig-Holstein</option>
<option>Thüringen</option>
</select>
</form>
</body>
</html>
I think you want to change the action in the form based on selected option.
Use the code bellow:
<!DOCTYPE html>
<html>
<body>
<form action="Bayern.html" id="form" name="form" accept-charset="UTF-8">
<input type="submit">
</form>
<select id="dropwdown" name="dropwdown" onchange="chgAction();">
<option selected>Bayern</option>
<option>Berlin</option>
<option>Brandenburg</option>
<option>Bremen</option>
<option>Hamburg</option>
<option>Hessen</option>
<option>Mecklenburg Vorpoomern</option>
<option>Niedersachsen</option>
<option>NRW</option>
<option>Rheinland</option>
<option>Saarland</option>
<option>Sachsen</option>
<option>Sachsen-Anhalt</option>
<option>Schleswig-Holstein</option>
<option>Thüringen</option>
</select>
<script>
function chgAction()
{
var selectedOption = document.getElementById("dropwdown").value;
if(selectedOption == "Hessen" ) {
document.getElementById('form').action = "Hessen.html";
}
else if(selectedOption == "NRW" ) {
document.getElementById('form').action = "NRW.html";
}
//Apply the same logic for each option ...
}
</script>
</body>
</html>
The code is running fine, but the if condition is not working, displays NA everytime the submit button is clicked.
Please find below the HTML and Javascript code.
Please check.
The HTML part-
<!DOCTYPE html>
<html>
<body>
<label style="color:blue;margin-left:30px;">Category:</label>
<input type="text" list="cat" />
<datalist id="cat">
<option value="Air Conditioner">
<option value="Chimney & Hoods">
</datalist>
<br><br>
<label style="color:blue;margin-left:30px;">Brand:</label>
<input type="text" list="brand" />
<datalist id="brand">
<option value="abc">
<option value="pqr">
<option value="xyz">
<option value="aaa">
<option value="bbb">
</datalist>
<br><br>
<button onclick="change()">Submit</button>
<br><br>
<label>Segmentation:</label>
<input type="text" id="segmentation" name="" value=""><br><br>
</body>
</html>
The Javascript part-
<script>
/*the javascript function not working correctly*/
function change() {
if (((document.getElementById("cat").value =='Air Conditioner') && ((document.getElementById("brand").value == 'abc')||(document.getElementById("brand").value =='pqr')))
|| ((document.getElementById("cat").value =='Chimney & Hoods') && ((document.getElementById("brand").value =='Glen')|| (document.getElementById("brand").value =='aaa')|| (document.getElementById("brand").value =='pqr'))))
{
document.getElementById("segmentation").value = 'Service';
} /*this condition not working*/
else {
document.getElementById("segmentation").value = 'NA';
}
}
Use `<input>` element's id to get the value of `<datalist>` element.
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
/*the javascript function not working correctly*/
function change() {
if (((document.getElementById("cat1").value =='Air Conditioner') && ((document.getElementById("brandName").value == 'abc')||
(document.getElementById("brandName").value =='pqr')))
|| ((document.getElementById("cat1").value =='Chimney & Hoods') && ((document.getElementById("brandName").value =='Glen')||
(document.getElementById("brandName").value =='aaa')||
(document.getElementById("brandName").value =='pqr'))))
{
document.getElementById("segmentation").value = 'Service';
}
else {
document.getElementById("segmentation").value = 'NA';
}
}
</script>
</head>
<body>
<label style="color:blue;margin-left:30px;">Category:</label>
<input type="text" list="cat" id="cat1" />
<datalist id="cat">
<option value="Air Conditioner">
<option value="Chimney & Hoods">
</datalist>
<br><br>
<label style="color:blue;margin-left:30px;">Brand:</label>
<input type="text" list="brand" id="brandName"/>
<datalist id="brand">
<option value="abc">
<option value="pqr">
<option value="xyz">
<option value="aaa">
<option value="bbb">
</datalist>
<br><br>
<button onclick="change()">Submit</button>
<br><br>
<label>Segmentation:</label>
<input type="text" id="segmentation" name="" value=""><br><br>
</body>
</html>
</body>
</html>
I'm having trouble trying to add the value part of the dropdown list to the end of my url as each value is different for the dropdown list.
<!DOCTYPE html>
<html>
<head>
<title>Task 8</title>
</head>
<body>
<script type="text/javascript">
change(val) {
document.staff1.action="http://tl28serv/task7.php?staffID=" + val;
}
</script>
<form id="StaffName" action="http://tl28serv/task7.php?staffID=" method="post">
<select id="staff1">
<option value="12">Perce Trainor</option>
<option value="15">Chuck Norris</option>
<option value="23">Jane Zumba</option>
</select>
<br/>
<input type="submit" name="submit">
</form>
</body>
</html>
You are missing the function keyword. Also you need to call the function change() on onchange event. Do it like this:
<script type="text/javascript">
function change(th) { //Added function keyword
document.getElementById("StaffName").action="http://tl28serv/task7.php?staffID=" + th.value;
}
</script>
<form id="StaffName" action="http://tl28serv/task7.php?staffID=" method="post">
<select id="staff1" onchange="change(this)"> <!--Called change() here-->
<option value="12">Perce Trainor</option>
<option value="15">Chuck Norris</option>
<option value="23">Jane Zumba</option>
</select>
<br/>
<input type="submit" name="submit">
</form>
Demo. Check console.
If you want to handle changed uri on server side and do nothing with it on client side, you don't have to do anything. just send data to server.
If you use GET method, your id appears on uri string.
<!DOCTYPE html>
<html>
<head>
<title>Task 8</title>
</head>
<body>
<form id="StaffName" action="http://tl28serv/task7.php" method="get">
<select id="staffID">
<option value="12">Perce Trainor</option>
<option value="15">Chuck Norris</option>
<option value="23">Jane Zumba</option>
</select>
<br/>
<input type="submit" name="submit">
</form>
</body>
first of all you are trying to set action on select box, that is not correct, you have to set action on form. I think this should work.
<!DOCTYPE html>
<html>
<head>
<title>Task 8</title>
</head>
<body>
<script type="text/javascript">
function change(val) {
document.StaffName.action="http://tl28serv/task7.php?staffID=" + val;
}
</script>
<form id="StaffName" action="http://tl28serv/task7.php?staffID=" method="post">
<select id="staff1" onSelect="change(this.value)">
<option value="12" >Perce Trainor</option>
<option value="15">Chuck Norris</option>
<option value="23">Jane Zumba</option>
</select>
<br/>
<input type="submit" name="submit">
</form>
</body>
</html>
You don't need JavaScript to achieve this.
Here is how you do it with HTML
Change your form method to GET, instead of POST.
Change form action to http://tl28serv/task7.php
Add name attribute to select, with value staffID
Of course, there exists a Javascript solution as well.
Here is how you achieve it:
<body>
<form id="StaffName" action="http://tl28serv/task7.php" method="post">
<select id="staff1">
<option value="12">Perce Trainor</option>
<option value="15">Chuck Norris</option>
<option value="23">Jane Zumba</option>
</select>
<br/>
<input type="submit" name="submit">
</form>
// move script
<script type="text/javascript">
var staff = document.getElementById('staff1');
// attach "change" EventListener to #staff1
staff.addEventListener('change', function() {
document.getElementById('StaffName').action="http://tl28serv/task7.php?staffID=" + this.value;
});
</script>
</body>
How can I get the value to be displayed after the selection from the list is made, without the use of a button. I tried to implement an onChange event for the select list, and doing away with the button, but I can't get it to work. I'd like the value (1,2,3) to display below the select box after an option (a,b,c) is selected.
<script>
function displayResult() {
this.nextSibling.nodeValue = document.getElementById("mySelect").value;
}
</script>
<body>
<form>
<select id="mySelect" style="width:220px">
<option value="1">a</option>
<option value="2">b</option>
<option value="3">c</option>
</select>
</form>
<button type="button" onclick="displayResult.call(this)">List Locations</button>
http://jsfiddle.net/8ZkR2/
If you are using JQuery you can do this:
HTML:
<form>
<select id="mySelect" style="width:220px">
<option value="1">a</option>
<option value="2">b</option>
<option value="3">c</option>
</select>
<div id="myDiv"></div>
</form>
JS:
$(document).ready(function() {
$('#mySelect').on('change', function () {
$('#myDiv').html(this.value);
});
});
Fiddle: http://jsfiddle.net/qv7cj/
Using jQuery:
$('#mySelect').change(function(){
//do stuff
})
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">
$(document).on('ready', function(){
$('#mySelect').on('change', function(){
console.log($('#mySelect').find('option:selected').attr('value'));
})
});
</script>
</head>
<body>`enter code here`
<form>
<select id="mySelect" style="width:220px">
<option value="1">a</option>`enter code here`
<option value="2">b</option>
<option value="3">c</option>
</select>
</form>
<button type="button" onclick="displayResult.call(this)">List Locations</button>
</body>
</html>
pls try this