I'm trying to hide a form after some input has been given, but when I use the .style.display = "none"; command, all that happens is clear the boxes.
Here is the context that I have, the JS is at the bottom. Thanks!
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form class = "input">
<label for = "name" required>Please enter your name</label>
<input type="text" id="name"><br><br>
<label for = "age" required>Please enter your age</label>
<input type="text" id="age"><br><br>
<label for = "colour">Please select a colour: </label>
<select id="colour" required>
<option value="Yellow">Yellow</option>
<option value="Blue">Blue</option>
<option value="Red">Red</option>
<option value="Green">Green</option>
</select><br><br>
<label for="">Select your difficulty:</label>
<select id="difficulty">
<option value = "1">1. Easy</option>
<option value = "2">2. Medium</option>
<option value = "3">3. Hard</option>
</select>
<br><br>
<input type="submit" onclick="clearScreen()">
</form>
<script>
function clearScreen() {
var x = document.getElementsByID("input");
x.style.display = none;
}
</script>
</body>
</html>
Try this:
HTML
<form id="theForm">
<!-- your form elements -->
<input type="submit" />
</form>
CSS
.hide {
visibility: hidden;
}
JavaScript
var theForm = document.getElementById("theForm");
theForm.addEventListener('submit', function(e) {
e.preventDefault();
this.classList.add("hide");
});
See this Fiddle: https://jsfiddle.net/dxwLr581/
I made three simple changes here. I changed form class="input" to id="input", I added onsubmit="return false" to the form element for the purposes of testing, and I changed the function call in the first line of your function to getElementById()
function clearScreen() {
var x = document.getElementById("input");
x.style.display = "none";
}
<form id="input" onsubmit="return false">
<label for="name" required>Please enter your name</label>
<input type="text" id="name"><br><br>
<label for="age" required>Please enter your age</label>
<input type="text" id="age"><br><br>
<label for="colour">Please select a colour: </label>
<select id="colour" required>
<option value="Yellow">Yellow</option>
<option value="Blue">Blue</option>
<option value="Red">Red</option>
<option value="Green">Green</option>
</select><br><br>
<label for="">Select your difficulty:</label>
<select id="difficulty">
<option value="1">1. Easy</option>
<option value="2">2. Medium</option>
<option value="3">3. Hard</option>
</select>
<br><br>
<input type="submit" onclick="clearScreen()">
</form>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form id = "input">
<label for = "name" required>Please enter your name</label>
<input type="text" id="name"><br><br>
<label for = "age" required>Please enter your age</label>
<input type="text" id="age"><br><br>
<label for = "colour">Please select a colour: </label>
<select id="colour" required>
<option value="Yellow">Yellow</option>
<option value="Blue">Blue</option>
<option value="Red">Red</option>
<option value="Green">Green</option>
</select><br><br>
<label for="">Select your difficulty:</label>
<select id="difficulty">
<option value = "1">1. Easy</option>
<option value = "2">2. Medium</option>
<option value = "3">3. Hard</option>
</select>
<br><br>
<input type="submit" onclick="clearScreen(event)">
</form>
<button onclick="toggleForm()">Toggle</button>
<script>
function clearScreen(e) {
var x = document.getElementById("input");
x.style.height = 0;
x.style.overflow = 'hidden'
}
function toggleForm() {
var x = document.getElementById("input");
if (x.style.height) {
x.style.height = '';
x.style.overflow = '';
} else {
x.style.height = 0;
x.style.overflow = 'hidden'
}
}
</script>
</body>
</html>
Related
I'm trying to develop a form where fields will be show according to already selected fields.
I'm facing problem to integrate JavaScript with html properly. I need your help to let me know how I can update the display of fields asynchronously.
Expected Behavior :
By default there will 1 choice selected and 1 input field , if user selects 2 choices from select input then there should be 2 input fields
This is minimal example where I'm trying:
document.getElementById("app").innerHTML = `
<h1>Show fields According to Selected Choice</h1>
<div>
1 field if selected one choice
2 fields if selected 2 choices
</div>
`;
body {
font-family: sans-serif;
}
<!DOCTYPE html>
<html>
<head>
<title>Parcel Sandbox</title>
<meta charset="UTF-8" />
</head>
<body>
<div id="app"></div>
<fieldset>
<div class="form-row field-type">
<div>
<label class="required" for="id_type">Select Choices:</label>
<select name="type" id="id_type">
<option value="1" selected>1 Choice</option>
<option value="2">2 Choices</option>
</select>
</div>
</div>
<div>
<label for="id_choice_1">Choice 1:</label>
<input
type="text"
name="choice_1"
class="vTextField"
maxlength="100"
id="id_choice_1"
/>
</div>
<div>
<label for="id_choice_2">Choice 2:</label>
<input
type="text"
name="choice_2"
class="vTextField"
maxlength="100"
id="id_choice_2"
/>
</div>
</fieldset>
<script>
function myFunction() {
var x = document.getElementById("id_type").value || null;
// Put logic here
}
</script>
<script src="src/index.js"></script>
</body>
</html>
I also added this into a sandbox if you want to run the code. https://codesandbox.io/s/fervent-worker-r6xszj?file=/src/index.js
Using the onchange event of the select you can call a function that first clears all the fields and then adds N fields as selected:
<html>
<head>
<title>Parcel Sandbox</title>
<meta charset="UTF-8" />
</head>
<body>
<div id="app"></div>
<fieldset>
<div class="form-row field-type">
<div>
<label class="required" for="id_type">Select Choices:</label>
<select name="type" id="id_type" onchange="genFields()">
<option value="1" selected>1 Choice</option>
<option value="2">2 Choices</option>
<option value="3">3 Choices</option>
<option value="4">4 Choices</option>
</select>
</div>
</div>
<div id="fields"></div>
</fieldset>
</body>
<script>
function genFields() {
document.getElementById("fields").innerHTML = "";
let numFields = document.getElementById("id_type").value;
for (let i = 1; i <= numFields; i++) {
document.getElementById(
"fields"
).innerHTML += `<div><label for='id_choice_${i}'>Choice ${i}</label><input type='text' id='id_choice_${i}' name='choice_${i}' class='vTextField' maxLength=100></div>`;
}
}
</script>
</html>
You can follow this:
let selects = document.querySelector("#id_type");
console.log(selects);
selects.onchange = function (e) {
let inputs = document.querySelector("#inputs");
inputs.innerHTML = `
<div>
<label for="id_choice_1">Choice 1:</label>
<input
type="text"
name="choice_1"
class="vTextField"
maxlength="100"
id="id_choice_1"
/>
</div>
`;
if(e.target.value == "2") {
inputs.innerHTML +=`
<div>
<label for="id_choice_1">Choice 2:</label>
<input
type="text"
name="choice_2"
class="vTextField"
maxlength="100"
id="id_choice_2"
/>
</div>
`;
}
}
<!DOCTYPE html>
<html>
<head>
<title>Parcel Sandbox</title>
<meta charset="UTF-8" />
</head>
<body>
<div id="app"></div>
<fieldset>
<div class="form-row field-type">
<div>
<label class="required" for="id_type">Select Choices:</label>
<select name="type" id="id_type">
<option value="1" selected>1 Choice</option>
<option value="2">2 Choices</option>
</select>
</div>
</div>
<div id="inputs">
<div>
<label for="id_choice_1">Choice 1:</label>
<input
type="text"
name="choice_1"
class="vTextField"
maxlength="100"
id="id_choice_1"
/>
</div>
</div>
</fieldset>
</body>
</html>
You just have to toggle the visibility of those elements with some logic to compare the selected option.
function myFunction(e) {
switch (e.target.value) {
case '1':
document.getElementById('id_choice_1_container').style.display = "block";
document.getElementById('id_choice_2_container').style.display = "none";
break;
case '2':
document.getElementById('id_choice_1_container').style.display = "none";
document.getElementById('id_choice_2_container').style.display = "block";
break;
default:
break;
}
}
<div id="app">
<h1>Show fields According to Selected Choice</h1>
</div>
<fieldset>
<div class="form-row field-type">
<div>
<label class="required" for="id_type">Select Choices:</label>
<select name="type" id="id_type" onchange="myFunction(event)">
<option value="1" selected>1 Choice</option>
<option value="2">2 Choices</option>
</select>
</div>
</div>
<div id="id_choice_1_container">
<label for="id_choice_1">Choice 1:</label>
<input type="text" name="choice_1" class="vTextField" maxlength="100" id="id_choice_1" onchange="myFunction(event)"/>
</div>
<div id="id_choice_2_container" style="display: none">
<label for="id_choice_2">Choice 2:</label>
<input type="text" name="choice_2" class="vTextField" maxlength="100" id="id_choice_2" onchange="myFunction(event)"/>
</div>
</fieldset>
i'm a total newbie to this.
I'd like to receive string in input the following:
Surname:
Name:
Country:
Region:
Email:
Phone number:
Special Requests:
Then I have to show them back in output when the button get pressed.
You can edit the code as much as you want because I think I made everything wrong.
The real problem is the output and the array.
What am I doing wrong (everything, I already know)?
Thanks.
var risposta;
var array = ["cognome", "nome", "comune", "regioni", "email", "telefono"]
function soluzione()
{
for (var i = 0; i < input.length; i++) {
var a = input[i];
k = k + "array[" + i + "].value= "
+ a.value + " ";
}
risposta = "Il numero รจ" +array+" Dai un altro numero.";
document.forms[1].risultato.value=risposta;
setTimeout(function(){window.location.reload()}, 3000);
}
#elaborato{
border-width:10px;
background-color:#FFFDC6;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="javascript.js"></script>
<center><td> <a href="http://www.istitutokennedy.net/">
<img alt="Istituto Kennedy" src="logokennedy.png"
width=400" height="120">
</a>
<td><h1>Form istituto Kennedy</h1></center>
</head>
<body bgcolor="#FFFDC6">
<div>
<table id="form" align="center">
<tr>
<br>
</tr>
<tr>
<td>
<form id="myForm" oninput="x.value=parseInt(a.value)+parseInt(b.value)">
Cognome: <INPUT type="text" style="padding-top:5px;" name="cognome" size=40><br><br>
Nome: <INPUT type="text" style="padding-top:5px;"name="nome" size=30><br><br>
Comune di Residenza: <INPUT type="text" style="padding-top:5px;"name="comune" size=12><br><br>
<label for="regioni">Regione:</label>
<select id="regioni" name="regioni">
<option value="Abruzzo">Abruzzo</option>
<option value="Basilicata">Basilicata</option>
<option value="Calabria">Calabria</option>
<option value="Campania">Campania</option>
<option value="Emilia-Romagna">Emilia-Romagna</option>
<option value="Friuli-Venezia Giulia">Friuli-Venezia Giulia</option>
<option value="Lazio">Lazio</option>
<option value="Liguria">Liguria</option>
<option value="Lombardia">Lombardia</option>
<option value="Marche">Marche</option>
<option value="Molise">Molise</option>
<option value="Piemonte">Piemonte</option>
<option value="Puglia">Puglia</option>
<option value="Sardegna">Sardegna</option>
<option value="Sicilia">Sicilia</option>
<option value="Toscana">Toscana</option>
<option value="Trentino-Alto Adige">Trentino-Alto Adige</option>
<option value="Umbria">Umbria</option>
<option value="Valle d Aosta">Valle d'Aosta</option>
<option value="Veneto">Veneto</option>
</select>
Email: <INPUT type="text" style="padding-top:5px;"name="email" size=30><br><br>
Telefono: <INPUT type="text" style="padding-top:5px;"name="telefono" size=15><br><br>
<label> Richieste Particolari </label>
<textarea rows="12" cols="60">
</textarea>
<INPUT type="button" class="myButton" value=" Soluzione " onClick="soluzione()"><br>
</form>
<form>
<br>Risultato: <INPUT type="text" style="padding-top:5px;" name="risultato" size=44><br>
</form>
</html>
I think you want to add a new input value to the array.
just use array_name.push(value)
For output use an iterator, like for loop or for each.
The best solution I could come up with is this
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Practice</title>
</head>
<body>
<form id="myForm">
Cognome: <input type="text" style="padding-top: 5px" name="cognome" size="40" /><br /><br />
Nome: <input type="text" style="padding-top: 5px" name="nome" size="30" /><br /><br />
Comune di Residenza: <input type="text" style="padding-top: 5px" name="comune" size="12" /><br /><br />
<label for="regioni">Regione:</label>
<select id="regioni" name="regioni">
<option value="Abruzzo">Abruzzo</option>
<option value="Basilicata">Basilicata</option>
<option value="Calabria">Calabria</option>
<option value="Campania">Campania</option>
<option value="Emilia-Romagna">Emilia-Romagna</option>
<option value="Friuli-Venezia Giulia">Friuli-Venezia Giulia</option>
<option value="Lazio">Lazio</option>
<option value="Liguria">Liguria</option>
<option value="Lombardia">Lombardia</option>
<option value="Marche">Marche</option>
<option value="Molise">Molise</option>
<option value="Piemonte">Piemonte</option>
<option value="Puglia">Puglia</option>
<option value="Sardegna">Sardegna</option>
<option value="Sicilia">Sicilia</option>
<option value="Toscana">Toscana</option>
<option value="Trentino-Alto Adige">Trentino-Alto Adige</option>
<option value="Umbria">Umbria</option>
<option value="Valle d Aosta">Valle d'Aosta</option>
<option value="Veneto">Veneto</option>
</select>
Email: <input type="text" style="padding-top: 5px" name="email" size="30" /><br /><br />
Telefono: <input type="text" style="padding-top: 5px" name="telefono" size="15" /><br /><br />
<label> Richieste Particolari </label>
<textarea rows="12" cols="60"> </textarea>
<input type="submit" class="myButton" value="Soluzione" /><br />
</form>
<br />Risultato:
<pre name="risultato" />
<script type="text/javascript" src="index.js" />
</body>
</html>
var risposta = {};
var array = ["cognome", "nome", "comune", "regioni", "email", "telefono"];
const formEl = document.getElementById("myForm");
formEl.addEventListener("submit", (e) => soluzione(e));
function soluzione(e) {
e.preventDefault();
for (var i = 0; i < array.length; i++) {
let value = document.getElementsByName(array[i])[0].value;
risposta[array[i]] = value;
}
document.getElementsByName("risultato")[0].innerHTML = JSON.stringify(risposta, null, 2);
}
If you can understand then perfect and if not then I can explain some parts in the comments too. Let me know :)
I was able to retrieve the input box values, but I am not sure how to retrieve the values from the drop down and radio button values using Java Script get method.
Here is my code:
INDEX PAGE
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css">
<title>Test</title>
</head>
<body>
<form action="Results.html" method="get">
<fieldset class = "form1">
<legend><b>Payment</b></legend>
<input type="radio" name="payment" value="Visa" id="Visa" checked> Visa
<input type="radio" name="payment" value="MasterCard" id="MasterCard"> MasterCard
<input type="radio" name="payment" value="AmericaExpress" id="AmericaExpress"> AmericaExpress
<input type="radio" name="payment" value="Discover" id="Discover"> Discover <br>
Card #: <input type="text" name="Card" required id="Card"><br>
Expiration:
<select id="Month">
<option value="01">01</option>
<option value="02">02</option>
</select>
<select id="Day">
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
<option value="2021">2021</option>
<option value="2022">2022</option>
</select>
<br>
CVV: <input type="text" name="cvv" id="Cvv" required> <br>
</fieldset>
<input type="submit" value="Submit" id="button1" name="submit"/>
</form>
</body>
</html>
Javascript/ Output Page
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Results</title>
<script>
var parseQueryString = function() {
var str = window.location.search;
var objURL = {};
str.replace(
new RegExp( "([^?=&]+)(=([^&]*))?", "g" ),
function( $0, $1, $2, $3 ){
objURL[ $1 ] = $3;
}
);
return objURL;
};
</script>
</head>
<body>
<h2> Your Form Has Been Submitted </h2>
<P> <b>Payment Information</b> </P>
<div class = "Visa"> Visa: </div>
<div class = "MasterCard"> Master Card: </div>
<div class = "AmericanExpress"> American Express: </div>
<div class = "Discover"> Discover: </div>
<div class = "Card"> Card Number: </div>
<div class = "Month"> Expiration Month: </div>
<div class = "Day"> Expiration Day: </div>
<div class = "cvv"> CVV: </div>
<script>
var params = parseQueryString();
var Visa = params["Visa"];
var MasterCard = params["MasterCard"];
var AmericanExpress = params["AmericanExpress"];
var Discover = params["Discover"];
var Card = params["Card"];
var Month = params["Month"];
var Day = params["Day"];
var cvv = params["cvv"];
console.log(Visa)
document.querySelector('.Visa').innerText += Visa;
console.log(MasterCard)
document.querySelector('.MasterCard').innerText += MasterCard;
console.log(AmericanExpress)
document.querySelector('.AmericanExpress').innerText += AmericanExpress;
console.log(Discover)
document.querySelector('.Discover').innerText += Discover;
console.log(Card)
document.querySelector('.Card').innerText += Card;
console.log(Month)
document.querySelector('.Month').innerText += Month;
console.log(Day)
document.querySelector('.Day').innerText += Day;
console.log(cvv)
document.querySelector('.cvv').innerText += cvv;
</script>
</body>
</html>
Card #: <input type="text" name="Card" required id="Card"><br>
Expiration:
<select id="Month">
<option value="01">01</option>
<option value="02">02</option>
</select>
<select id="Day">
<option value="2018">2018</option>
<option value="2019">2019</option>
</select>
<br>
CVV: <input type="text" name="cvv" id="Cvv" required> <br>
</fieldset>
<input type="submit" value="Submit" id="button1" name="submit"/>
</form>
</body>
</html>
The results.html page displays the values from the input type that are text, but doesn't return the value from the radio and the drop down menu. Please help me with this. Thanks in advance.
For Radio buttons and Drop down's parameter name in query string is sent as name of the control.
You need to define a name for Month and Year. Card Type is sent as payment in query string as you gave the name as payment.
Index Page
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css">
<title>Test</title>
</head>
<body>
<form action="Results.html" method="get">
<fieldset class = "form1">
<legend><b>Payment</b></legend>
<input type="radio" name="payment" value="Visa" id="Visa" checked> Visa
<input type="radio" name="payment" value="MasterCard" id="MasterCard"> MasterCard
<input type="radio" name="payment" value="AmericaExpress" id="AmericaExpress"> AmericaExpress
<input type="radio" name="payment" value="Discover" id="Discover"> Discover <br>
Card #: <input type="text" name="Card" required id="Card"><br>
Expiration:
<select id="Month" name="Month">
<option value="01">01</option>
<option value="02">02</option>
</select>
<select id="Day" name="Day">
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
<option value="2021">2021</option>
<option value="2022">2022</option>
</select>
<br>
CVV: <input type="text" name="cvv" id="Cvv" required> <br>
</fieldset>
<input type="submit" value="Submit" id="button1" name="submit"/>
</form>
</body>
</html>
Results Page
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Results</title>
<script>
var parseQueryString = function() {
var str = window.location.search;
var objURL = {};
str.replace(
new RegExp( "([^?=&]+)(=([^&]*))?", "g" ),
function( $0, $1, $2, $3 ){
objURL[ $1 ] = $3;
}
);
return objURL;
};
</script>
</head>
<body>
<h2> Your Form Has Been Submitted </h2>
<P> <b>Payment Information</b> </P>
<div class = "paymentType"> Payment Type: </div>
<div class = "Card"> Card Number: </div>
<div class = "Month"> Expiration Month: </div>
<div class = "Day"> Expiration Day: </div>
<div class = "cvv"> CVV: </div>
<script>
var params = parseQueryString();
var paymentType = params["payment"];
var Card = params["Card"];
var Month = params["Month"];
var Day = params["Day"];
var cvv = params["cvv"];
document.querySelector('.paymentType').innerText += paymentType;
console.log(paymentType)
document.querySelector('.Card').innerText += Card;
console.log(Month)
document.querySelector('.Month').innerText += Month;
console.log(Day)
document.querySelector('.Day').innerText += Day;
console.log(cvv)
document.querySelector('.cvv').innerText += cvv;
</script>
</body>
</html>
Card #: <input type="text" name="Card" required id="Card"><br>
Expiration:
<select id="Month">
<option value="01">01</option>
<option value="02">02</option>
</select>
<select id="Day">
<option value="2018">2018</option>
<option value="2019">2019</option>
</select>
<br>
CVV: <input type="text" name="cvv" id="Cvv" required> <br>
</fieldset>
<input type="submit" value="Submit" id="button1" name="submit"/>
</form>
</body>
</html>
This are two different things, that can be retrieved very easily, as soon as you experiment a bit around.
See the following simplified examples:
console.log(masterCard.checked)
console.log(day.value)
<input type="radio" id="masterCard" checked>
<select id="day">
<option value="2018" selected></option>
</select>
Side note: This is a standard requirement for clean scripting, use camelCase to name an id, mostly don't use uppercase in the first char for id!
yeah im having some trouble with this, it wont calculate the prices and i was hoping some one would could help me please
function calculatePrice(myForm){
//Get selected data
var elt = document.getElementById("tickets1");
var tickets1 = elt.options[elt.selectedIndex].value;
var elt = document.getElementById("tickets2");
var tickets2 = elt.options[elt.selectedIndex].value;
//convert data to integers
tickets1 = parseInt(tickets1);
tickets2 = parseInt(tickets2);
//calculate total value
var total = tickets1 + tickets2;
//print value to PicExtPrice
document.getElementById("PicExtPrice").value=total;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../CSS/stylepage.css">
<script type="text/javascript" src="../JS/prcecal.js">
</script>
</head>
<center>
<body>
<fieldset>
<div id="box_1">
<center><h2>Order</h2></center>
<form name="myForm">
Email:
<br>
<input type="email" name="email" id="email" required />
<br>
<br>
Date:
<br>
<input type="date" name="date" id="date" min="today" required />
<br>
<br>
<div id="dropdowns">
<SELECT NAME="Ticketsadults" onChange="calculatePrice()" id="tickets1">
<OPTION value="0">0</OPTION>
<OPTION value="20">1</OPTION>
<OPTION value="40">2</OPTION>
<OPTION value="60">3</OPTION>
<OPTION value="80">4</OPTION>
</SELECT>
<br>
<SELECT NAME="Ticketskids" onChange="calculatePrice()" id="tickets2" >
<OPTION value="0">0</OPTION>
<OPTION value="20">1</OPTION>
<OPTION value="40">2</OPTION>
<OPTION value="60">3</OPTION>
<OPTION value="80">4</OPTION>
</SELECT>
</div>
<br>
<br>
<br>
<button type="button" onclick="calculatePrice()">Calculate</button>
<INPUT type="text" id="PicExtPrice" Size=8>
</form>
</center>
</div>
</fieldset>
</body>
</html>
Ive tried changing it around and stuff but it still wont calcualte the two select drop downs
You put parseInt(tick1) and parseInt(tick2) instead of parseInt(tickets1) and parseInt(tickets2).
Also, you put tick1 + tick2 instead of tickets1 + tickets2.
I'm trying to include in the option, that if the user chooses/clicks the Other please specify option, he gets an option to give input. The characters thus input will instantly show relevant nationalities in a dropdown bar.
For a javascript function based solution for this, how can I make a call to that function in my html code or in dropdown. Thanks! :D
<div class="form-group">
<label for="status" class="control-label col-xs-4">
<p class="left">Nationality</p>
</label>
<select name="status" required>
<option></option>
<option value="" disabled selected>Filipino</option>
<option value='Filipino'>Filipino</option>
<option value='American'>American</option>
<option value='Japanese'>Japanese</option>
<option value='French'>French</option>
<option value='Sweden'>Sweden</option>
<option value='other'>Others please specify.</option>
</select>
</div>
Check the below code
var code = $('#code'); //input field
code.hide(); //initially hide
$("#status").on('change', function() {
var selected_option = $('#status option:selected').eq(0);
if (selected_option.val() == 'other') {
code.show();
code.val(''); //empty the field every-time selecting the option
console.log(selected_option.val());
} else {
code.hide();
}
console.log(selected_option.val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
<label for="status" class="control-label col-xs-4">
<p class="left">Nationality</p>
</label>
<select name="status" id="status" required>
<option></option>
<option value="" disabled selected>Filipino</option>
<option value='Filipino'>Filipino</option>
<option value='American'>American</option>
<option value='Japanese'>Japanese</option>
<option value='French'>French</option>
<option value='Sweden'>Sweden</option>
<option value='other'>Others please specify.</option>
</select>
<input type="text" name="code" id="code" />
</div>
Below is the full implementation
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Nationality</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<div class="form-group">
<label for="status" class="control-label col-xs-4">
<p class="left">Nationality</p>
</label>
<select name="status" id="status" required="required">
<option></option>
<option value="" disabled="disabled" selected="selected">Filipino</option>
<option value='Filipino'>Filipino</option>
<option value='American'>American</option>
<option value='Japanese'>Japanese</option>
<option value='French'>French</option>
<option value='Sweden'>Sweden</option>
<option value='other'>Others please specify.</option>
</select>
<input type="text" name="code" id="code">
</div>
<script>
var code = $('#code'); //input field
code.hide(); //initially hide
$("#status").on('change', function() {
var selected_option = $('#status option:selected').eq(0);
if (selected_option.val() == 'other') {
code.show();
code.val(''); //empty the field every-time selecting the option
console.log(selected_option.val());
} else {
code.hide();
}
console.log(selected_option.val());
});
</script>
</body>
</html>
Try this:
You need to have a listener for your select and take the action as per the selected value..
var inputLabel = $('#inputLabel');
var onOptionChange = function() {
if (this.value == 'other') {
inputLabel.fadeIn(100);
} else {
inputLabel.fadeOut(100);
}
};
$('#status').on('change', onOptionChange);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="form-group">
<label for="status" class="control-label col-xs-4">Nationality</label>
<select name="status" id="status" required>
<option value="" disabled selected>Select your Nationality</option>
<option value='Filipino'>Filipino</option>
<option value='American'>American</option>
<option value='Japanese'>Japanese</option>
<option value='French'>French</option>
<option value='Sweden'>Sweden</option>
<option value='other'>Others please specify.</option>
</select>
<label for="input" id="inputLabel" style="display: none">Other:
<input type="text" id="input">
</label>
</div>