Select field or Text field - html

I have a select with many options. If a user doesn't find value in option, He could fill a "other" field :
<select name="value">
<option>Choose...</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="text" name="value">
In this case, if I choose a value in the select, it appeared blank beacause of the text fiel.
Thanks

I imagined it, needs to be improved
http://jsfiddle.net/neiesc/jv6qgn03/
<select name="value">
<option>Choose...</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<br />
<label for="chkOther">
Other
<input id="chkOther" type="checkbox" value="0" />
</label>
<input id="txtOther" type="text" name="value" disabled>
Javascript code:
$( "#chkOther" ).click(function() {
$( "#txtOther" ).prop( "disabled", !$( "#txtOther" ).prop( "disabled"));
});

Use different names for select and text input.
<form method="post">
<select name="select">
<option>Choose...</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="text" name="field">
<input type="submit" name="submit">
</form>
And in PHP check what was chosen:
<?php
if (isset($_POST['submit'])) {
// form was sent
if (!empty($_POST['select'])) {
// select or both, use select
} elseif (!empty($_POST['input'])) {
// empty select txt input filled
} else {
// nothing chose in select and text input is blank
}
}
?>

Related

Html button and values

I am a backend developer so I need help with this front end part.
<input id="entered_value" type="number" name="entered_value">
<input id="entered_valuetwo" type="text" name="entered_valuetwo">
<select id="selected_value" name="selected_value"> <option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<button type="button" id="send-data">Click me</button>
when the button is clicked its already triggering something I have in JS. But what is the best way to pass "entered_value" & "entered_valuetwo" & "selected_value" all 3 at the same time to the button and they get passed to the js function thereafter.
just add this to your js
document.getElementById('send-data').addEventListener('click',buttonFunction)
function buttonFunction(){
let enteredValue = document.getElementById('entered_value').value
let enteredValue2 = document.getElementById('entered_valuetwo').value
let selectValue = document.getElementById('selected_value').value
console.log(enteredValue,enteredValue2,selectValue)
}
<input id="entered_value" type="number" name="entered_value">
<input id="entered_valuetwo" type="text" name="entered_valuetwo">
<select id="selected_value" name="selected_value"> <option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<button type="button" id="send-data">Click me</button>
If you want to put it into a databas i would use post request like this:
<form action="your url" method="Post">
<input id="entered_value" type="number" name="entered_value">
<input id="entered_valuetwo" type="text" name="entered_valuetwo">
<select id="selected_value" name="selected_value">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<button type="button" id="send-data">Click me</button>
</form>
But if you want to use it temporarylie use document.getElementby(𝗜𝗗) or better document.querySelector(#𝗜𝗗)

Collecting select values into an array (jQuery)

I've been attempting to collect the selected data value of each of the dropdowns in a form and pass them into an array so that I can eventually add them together. However with my current code, the array only receives the value of the first dropdown, and not any of the others. I haven't got to the addition part yet, so I'm just joining the array with a comma, but here's my code so far:
HTML
<form id="questions">
<select name="q1" id="q1" tabindex="1">
<option data-calculate="0" value="No">No</option>
<option data-calculate="0.5" value="Partially">Partially</option>
<option data-calculate="1" value="Yes">Yes</option>
</select>
<select name="q2" id="q2" tabindex="2">
<option data-calculate="0" value="No">No</option>
<option data-calculate="0.5" value="Partially">Partially</option>
<option data-calculate="1" value="Yes">Yes</option>
</select>
<select name="q3" id="q3" tabindex="3">
<option data-calculate="0" value="No">No</option>
<option data-calculate="0.5" value="Partially">Partially</option>
<option data-calculate="1" value="Yes">Yes</option>
</select>
<input type="hidden" id="calculator" value=""/>
</form>
JQUERY
var calculation = [];
jQuery("#questions").change(function() {
calculation.push(jQuery(this).find(":selected").data('calculate'));
jQuery("#calculator").val(calculation.join(', '));
});
Thanks in advance for the help!
Consider the following.
$(function() {
$("#questions > select").change(function(event) {
var calc = [];
$("#questions > select").each(function(i, el) {
calc.push($(el).val());
});
$("#calculator").val(calc.join(", "));
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="questions">
<select name="q1" id="q1" tabindex="1">
<option data-calculate="0" value="No">No</option>
<option data-calculate="0.5" value="Partially">Partially</option>
<option data-calculate="1" value="Yes">Yes</option>
</select>
<select name="q2" id="q2" tabindex="2">
<option data-calculate="0" value="No">No</option>
<option data-calculate="0.5" value="Partially">Partially</option>
<option data-calculate="1" value="Yes">Yes</option>
</select>
<select name="q3" id="q3" tabindex="3">
<option data-calculate="0" value="No">No</option>
<option data-calculate="0.5" value="Partially">Partially</option>
<option data-calculate="1" value="Yes">Yes</option>
</select>
<input type="hidden" id="calculator" value="" />
</form>
Each change to a select element triggers the callback. Making a more global variable will cause it to get updated over and over again if the User makes many changes or changes their selection. This will capture all 3 values and join them. If the user changes a selection, the hidden field is updated with just the values currently selected.

How can I get data for select box and set the data while using searching?

<div>
<label for="sort">sort</label>
<select name="sort" id="sort" class="form-control">
<option value=""></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div>
<div>
<label for="label">label</label>
<select name="label" id="label" class="form-control">
<option value=""></option>
<option value="label1">label1</option>
<option value="label2">label2</option>
</select>
</div>
There is a search function, so when I choose the sort and label select box then click the search button, it will sort data. But the selected value is not set in the select box.
How can I solve this? Please help me to solve this.
Add any event to get the selected dropdown value. Below, I have added a select change event.
$("#label").on("change", function(){
//get the data using the above ID's
var cars = ["Saab", "Volvo", "BMW"]; //example array
//appending to the new Select #newData
var htmlContent = "";
htmlContent = "<option value=''></option>";
$.each( cars, function( i, val ) {
$( "#" + val ).text( "Mine is " + val + "." );
htmlContent += "<option value= "+val+">"+val+"</option>";
});
$("#newData").append(htmlContent);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
<label for="sort">sort</label>
<select name="sort" id="sort" class="form-control">
<option value=""></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div>
<div>
<label for="label">label</label>
<select name="label" id="label" class="form-control">
<option value=""></option>
<option value="label1">label1</option>
<option value="label2">label2</option>
</select>
</div>
<div>
<label for="newData">New Data</label>
<select name="newData" id="newData" class="form-control">
</select>
</div>

Show second value from select option

I want show in input text from select option where in value have 2 data
function changeHiddenInput (objDropDown)
{
document.getElementById("hiddenInput").value = objDropDown.value;
}
<form name="ff">
<select id="dropdown" name="dropdown" onchange="changeHiddenInput(this)">
<option value="j.hotmail.com|3">Jens</option>
<option value="a.hotmail.com|4">Adam</option>
<option value="d.homtail.com|5">Dan</option>
</select>
<input type="text" type="hidden" name="hiddenInput" id="hiddenInput" />
</form>
I want get value 3,4,5 and show in input text form when each option select.
split your value using " | "
function changeHiddenInput (objDropDown)
{
document.getElementById("hiddenInput").value = objDropDown.value.split('|')[1];
}
<form name="ff">
<select id="dropdown" name="dropdown" onchange="changeHiddenInput(this)">
<option value="j.hotmail.com|3">Jens</option>
<option value="a.hotmail.com|4">Adam</option>
<option value="d.homtail.com|5">Dan</option>
</select>
<input type="text" type="hidden" name="hiddenInput" id="hiddenInput" />
</form>

Booking mask - Changing the type="date" value to type="text" when submit

I'm working on a hotel website and received a code from TravelClick for the booking mask. My client didn't like the "pop up calendar" so I tried implementing a "type="date" in the code instead. But when I click submit, it doesn't recognized the date anymore. I think the problem is that before the type value was "text" and now it's "date", but how can I change it back to "text" without losing the calendar?
<form action="https://booking.ihotelier.com/istay/istay.jsp" method="get" name="resform" id=resform"">
<input type="hidden" name="HotelID" value="12401" />
<input type="hidden" name="LanguageID" value="3" />
<input type="hidden" name="Rooms" value="1" />
Check-In Date:
<input type="date" name="DateIn" />
Nights:
<select name="Length">
<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="10">11</option>
<option value="10">12</option>
<option value="10">13</option>
<option value="10">14</option>
</select>
Adults:
<select name="Adults">
<option value="1" selected="selected">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
Children:
<select name="Children">
<option value="0" selected="selected">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<input type="submit" value="Submit" />
</form>
<!-- This javascript code populates the Check-In Date field with today's date. It must be located after the form close tag. -->
<script type="text/javascript" language="javascript">
function getDateStr() {
var today = new Date();
var todayStr = (today.getMonth()+1) + "/" + today.getDate() + "/" + today.getFullYear();;
document.resform.DateIn.value = todayStr;
}
getDateStr();
</script>