I want to set the select option as how the input is.
In input tag we can do this:
<input type="number" max="100" min="0"/>
And now I want to do this in select option:
<select>
<option max="100" value="100">100</option>
<option max="200" value="200">200</option>
<select>
The reason is I just want to ensure if the value which is selected by user is appropriate as real value.
I don't fully understand what you are asking, but I'm going to assume you are asking whether there are alternatives to using select.
If you are looking to allow users to select a range of numbers (eg. 100-200), you can choose to use a Html Slider Bar. There are plenty of simple tutorials out there that teaches you how to use a slider bar.
An example of a HTML5 Slider Bar Tutorial.
Otherwise, if you want a fixed value (eg. 100 OR 200), a select option will work perfectly fine.
I find the way to check is the selected option correct in case when not every options in the template are corrects.
You have to use CustomValidity, like this:
<select id="select">
<option value="100">100</option>
<option value="200">200</option>
<select>
But, because of some reason now the only 100 is valid
let select = document.querySelector('#select');
let setSelectValidity = function () {
if (select.value !== 100) {
select.setCustomValidity('The only 100 is available');
else {
select.setCustomValidity('');
}
}
select.addEventListener('change', setSelectValidity);
Related
I have a select element and no matter what I try, the option values are blank. The number of options in the drop down is correct, but they are blank. Here is the html:
<label for="accountBrokerageName">Brokerage:</label>
<select id="accountBrokerageName">
<option value="Interactive Brokers, LLC"></option>
<option value="Vanguard"></option>
</select>
I'm assuming some css from another library is overriding the standard select>option css. I've tried commenting out each linked library one at a time but no joy. I've tried adding a .clear-css class to the option like this:
.clear-css {
all:unset;
}
and append it to all options using jquery just before it is used, like this:
$('option').addClass('clear-css');
Still blank.
I've also tried
.clear-css {
all:initial;
}
Still blank.
I also tried adding the clear-css class to the select element, but that causes the whole select element to disappear.
Any suggestions?
You need to include the values for each option between the opening and closing <option> tags. The value doesn't need to match the text content. In fact, it's usually better to remove any special characters and even spaces when working with external APIs, like this:
// This JS is just for the sake of example, to log the new value with each change
const select = document.getElementById('accountBrokerageName');
select.addEventListener('change', () => console.log(select.value));
<label for="accountBrokerageName">Brokerage:</label>
<select id="accountBrokerageName">
<option value="" disabled selected>Choose an option...</option>
<option value="interactive-brokers-llc">Interactive Brokers, LLC</option>
<option value="vanguard">Vanguard</option>
</select>
Select values must be within the actual <option> tags:
<label for="accountBrokerageName">Brokerage:</label>
<select id="accountBrokerageName">
<option value="Interactive Brokers, LLC">Interactive Brokers, LLC</option>
<option value="Vanguard">Vanguard</option>
</select>
I'm looking for a way to set a default value (in a select list) that has to be switched out of in order for the 'required=""' attribute to be accepted and for the form to be submitted. In other words the default value can't stay as the chosen value. Also, how would one add titles to a select list that can not be chosen and are meant as a way of labeling the select options. Everything I am looking for is shown in the image below in case of confusion.
In other words the default value can't stay as the chosen value.
About this request, you could find this helpful.
<form action="/action_page.php">
<select id="mySelect" required onchange="changeValue()">
<option value="">Select a Country</option>
<option value="c1">Country 1</option>
<option selected value="c2">Country 2</option>
<option value="c3">Country 3</option>
<option value="c4">COuntry 4</option>
</select>
<input id="btnSubmit" type="submit" disabled>
</form>
<script>
function changeValue(){
if (document.getElementById("mySelect").value != "")
document.getElementById("btnSubmit").disabled = false;
}
</script>
If your selected value changes (just after the first time) and it's different from "", the submit button will be valid. This can also be accomplished with a flag instead of setting the property disable, it depends on what you need to do.
You could try checking with javascript if the value = ""
Example from w3Schools.com:
https://www.w3schools.com/js/js_validation.asp
Make sure you also validate on the server side! people could change the HTML / Javascript
Is it possible for <select>-lists to reference the same list of options, similar to <input> with <datalist>?
I generate a list of several entries, where (among other things) the user selects a value of a dropdownlist. The options in this list are the same for each entry, so I would prefer it, if the list of options doesn't need to be re-added for each dropdownlist.
I can't use <input> with <datalist>, since the user may only choose from available entries.
you could do this using jquery easily,
<datalist id="mylist">
<option value="a">
<option value="b">
<option value="b">
</datalist>
<select class="someSelect">
<select class="someSelect">
$(".someSelect").html( $("#mylist").html() );
this would replace all your select list from the datalist
This is not realy the good answer. There is a big difference between 'datalist' and 'select' which for as far as I read till yet stays unspoken : in a select the 'value' can be different from the visualized 'innerHTML', which is not the case in a datalist. So what we need is a kind of 'select' with an attribute like 'selectlist="countries' the selectlist then would look like this :
<selectlist>
<option value='1'>Belgium</option>
<option value='2'>France</option>
</selectlist>
and can be reused in more then one 'select' and send the value back to the server instead of the innerHTML.
What's happening is that I'm using a payment processor not under my control, and I need to pass it the values of shipping, and the values of shipping per additional item.
Since they're packaged separate, we're going to charge the same amount per additional item, so instead of coming up with two identical fields that the user has to fill out, I'm trying to create one field that assigns the value of shippingf and shipping2f
Below is an example of one of my many attempts. Another attempt has been just putting name="" twice, and that didn't seem to work either.
<select name="shippingf, shipping2f" style="height:35px;">
<option value="12">US</option>
<option value="32">Canada</option>
</select>
any help on this matter would be fantastic.
Thanks!
Not possible with just HTML.
You could either
use JS to change the value of a type="hidden" element in the form, or
server-side (assuming PHP), before you include your payment processor, add $_POST['shipping2f'] = $_POST['shippingf'];. Just keep shippingf in the HTML.
Since the OP said option 2 won't work, here's an example for option 1:
HTML:
<select name="shippingf" style="height:35px;">
<option value="12">US</option>
<option value="32">Canada</option>
</select>
<input name="shipping2f" type="hidden"></input>
jQuery:
$("select[name=shippingf]").change(function(){
$("input[name=shipping2f]").val($(this).children(':selected:first').val());
}).change();
Demo: http://jsfiddle.net/nb8j06qb/
or <====
HTML:
<select id='sf' name="shippingf" style="height:35px;">
<option value="12">US</option>
<option value="32">Canada</option>
</select>
<input id='s2f' name="shipping2f" type="hidden" value="12"></input>
Vanilla JS:
document.getElementById('sf').onmouseup = function(){
document.getElementById('s2f').value = this.options[this.selectedIndex].value;
};
Demo: http://jsfiddle.net/nb8j06qb/1/
I have a list of people sorted by 2 parameters that I insert in a select.
How can, when someone select a parameter, maintain this one selected when he refresh or change/return on the page?
I see some issues on some post over the internet, but most of them use JQuery, and I don't want to use it.
The code is like this:
<select name="idactivity_contact[]" id="_activity" multiple="multiple" size="10" style="width:150px;">
<option disabled="disabled" style="background-color:#dddddd;font-weight:bold;">Shipbroking</option>
<option value="1">Newbuilding</option>
<option value="2">Sales and Purchase</option>
<option value="3">Bulk</option>
</select>
The code is generated in PHP actyally manually, but i will do a function later
One way is to use javascript and localStorage:
document.getElementById("_activity").onchange = function() {
localStorage.setItem('selectedtem', document.getElementById("_activity").value);
}
if (localStorage.getItem('item')) {
document.getElementById("selectedtem").options[localStorage.getItem('selectedtem')].selected = true;
}
You can also use cookies or session in your sever side as well.
Most direct approach would be to store the values of $_POST[] in $_SESSION[].