how to expand html multiple select box when clicked - html

Is it possible to get a multiple select box to behave like a select box in the following way: Only show selection box (1 row) and expand when clicked?
<select multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
but I only want to see the empty 1 row select box until clicked and then have it expand as a dropdown with scroll bar if needed

I hope CSS is enough to solve your problem. Try something like this.
select{height:20px}
select:focus{height:auto}
<select multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>

This accomplishes what you are looking for at a very basic level using jQuery. All options are initially hidden with CSS except the first option. Using jQuery, once the first option is clicked, then the remaining options will display.
Working Example: https://jsfiddle.net/byronj/fmvufoxz/4/
**HTML:**
<select multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
**CSS:**
select option {
display: none;
}
select option:first-of-type, .show {
display: block;
}
**jQuery:**
jQuery(document).ready(function ($) {
$('option').on( "click", function() {
$('option').show();
});
});

I believe what you are trying to do can be accomplished by:
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
Try running the above snippet and see if that's what you were looking for.
Hope that helped!

Related

how to display select box options without selecting select box

There is a requirement in my project like.,
I have select box in desktop view and in mobile only options has to be displayed.
I got stuck how to display all the options without selecting select box .
<h3>Desktop View</h3>
<select name="carmakes" id="carmakes">
<option selected='selected' value="0">Car makes</option>
<option value="bmw">BMW</option>
<option value="audi">Audi</option>
</select>
<br/><br/><br/><br/>
<h3>Mobile View</h3>
<option selected='selected' value="0">Car makes</option>
<option value="bmw">BMW</option>
<option value="audi">Audi</option>
Thanks
Demo
Just give multiple for your select box and it'll display like that...
<select multiple name="carmakes" id="carmakes">
<option selected='selected' value="0">Car makes</option>
<option value="bmw">BMW</option>
<option value="audi">Audi</option>
</select>

Selecting an option in drop down menu one determines list available in the second drop down menu

I want to make the contains of a second drop down menu dependent on what is selected in the the first. In my case wish to do it for a car selection option. i.e when the car make is selected this then populates the model drop down menu with the relevant models.
As there are a lot of makes of car I would like to keep it concise if possible I presume arrays of models for each make is the solution but how do implement this.
I have look at various solutions and cannot find a suitable answer to this, any suggestions/examples would be gratefully appreciated.
I am using jsp.
Make:<select>
<option value="audi">Audi</option>
<option value="bmw">BMW</option>
<option value="ford">Ford</option>
<option value="toyota">Toyota</option>
</select>
You could do it with some simple JS and css ( here is a JSFiddle: http://jsfiddle.net/N7Q57/1/)
Make:
<select name="cars" id="cars">
<option selected value="Choose">Choose</option>
<option value="Audi">Audi</option>
<option value="BMW">BMW</option>
<option value="Ford">Ford</option>
<option value="Toyota">Toyota</option>
</select>
<div id="brands">
<select class="audi">
<option value="1">1</option>
<option value="2">2</option>
</select>
<select class="bmw">
<option value="3">3</option>
<option value="4">4</option>
</select>
</div
the jQuery
$("#cars").change(function () {
var string = "";
string = $("select#cars option:selected").text();
if (string == "Audi"){
$("#brands .bmw").hide();
$("#brands .audi").show();
$("#brands").show();
} else if (string == "BMW"){
$("#brands .audi").hide();
$("#brands .bmw").show();
$("#brands").show();
}
})
and the CSS:
#brands{
display: none;
}

How to avoid showing the first value inside HTML Select tag

This is my simple HTML Select Program ,
by default its showing value as Volvo (the first value )
Is it possible that , not to show any value and let the user select ??
<!DOCTYPE html>
<html>
<body>
<form action="demo_form.asp">
<select name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<input type="submit">
</form>
</body>
</html>
It's really simple. Just add a blank option:
<form action="demo_form.asp">
<select name="cars">
<option></option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<input type="submit">
</form>
Add a blank option to the first position. Be sure to give it a value that you can then check for in your submit handler. EDIT: Or just use selectedIndex.
i.e.
if(document.getElementById("select1").selectedIndex != 0){
form1.submit();
}
else {alert("Please select a value!");}
to validate they have selected something.
<form name="form1" id="form1" action="demo_form.asp">
<select id="select1"name="cars">
<option value="none"></option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<input type="submit">
</form>
There are two ways:
1) Disable first item:
<select name="cars">
<option value="" disabled>[Select an item]</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
2) Make first item empty, as already suggested.
<select name="cars">
<option value=""></option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
create a div (i called it "noShow"),
<div class="noShow">
<form action="demo_form.asp">
<select name="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<input type="submit">
</form>
</div> <!-- end noShow DIV
then, using Style sheets (CSS), apply this to the cars
.noShow option:first-child{
display:none;
try that, good luck
There's no "clean" way to do what you are asking - an HTML select will always display one of the values.
You can, however, include a 'Not Selected' option:
<option value=''>--Not Selected--</option>
but then you also have to do some validation to make sure that the user actually picks a valid value. That's a topic for a different question. :)
Easiest way is to do something like this, if you also wanna provide some information:
<form action="demo_form.asp">
<select name="cars">
<option value="default">Pick Something!</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<input type="submit">
</form>

how to set a default prompt when nothing selected for "select" using css

Is it possible to set a default prompt when nothing is selected for select box using css? or is there an elegant way to deal with this other than
<option>Select Here</option>?
Try this:
<select>
<option value="" disabled>Select one--</option>
<option value="someVal1">Option 1</option>
<option value="someVal2">Option 2</option>
<option value="someVal3">Option 3</option>
<option value="someVal4">Option 4</option>
</select>
The accepted answer did not work for me as expected.
If you need the "Select one option" to be the selected one by default , this worked for me
<select id="selected_option" name="selected_option">
<option value="default" selected="selected">Select one option </option>
<option value="someVal1">Option 1</option>
<option value="someVal2">Option 2</option>
<option value="someVal3">Option 3</option>
<option value="someVal4">Option 4</option>
</select>
Is also important that the select has id and name so when you submit the form the value will be part of the POST or GET
<select>
<option style="display:none">Empty</option>
<option>button1</option>
<option>button2</option>
</select>
This is how I use for setup the default prompt but it will not set a default value. It use a HTML5 validation required. It may be a little bit shift from the question but I hope it could help as alternative.
<select id="car" name="car[brand]" required>
<option disabled selected>Select Car Brand</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
Try using option with unset value
<select>
<option value>Select something</option>
<option value="1">Foo</option>
<option value="2">Bar</option>
</select>

Select option tag with "Please select" message

I want a dropdown list with say the following options and to show please select when no option is selected. I have implemented it with the code below and the form is submitted when an option is selected as expected.
The problem is when I navigate back to the page, and select the first option i.e --Please Select--, my form gets submitted for that option too. Is there any way to display a --Please Select-- option when no option is selected and prevent the form from being submitted for that particular --Please Select-- option?
<select onchange="this.form.submit()">
<option>--Please Select--</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
You can use <option selected="selected" disabled="true">
Selected means it will be default, and then disabled stops it being clicked on.
Demo
<select onchange="this.form.submit()">
<option selected="selected" disabled="true">--Please Select --</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
Make the "Please Select" option disabled. This way the option will be initially selected but it can't be selected again.
<select onchange="this.form.submit()">
<option disabled="true">--Please Select --</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
Here you go:)
<select onchange="if(this.value!='') this.form.submit();">
<option value="">--Please Select --</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
you could do it in javascript.
<select onchange="if (this.selectedIndex !=0) { this.form.submit(); }">
<option>--Please Select --</option>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
since this is a php you're probably dynamically generating your select options so you might need to change the code a bit.