Dropdown select extended options - html

Im trying to make a dropdown. U have to search on the package number and you will get the detailed information so you can immediately see the correct price or sizes for that package.
<div class="choosePackage">
<label>
<span>Package nr</span>
<span>m3</span>
<span>Size LxWxH</span>
<span>Price</span>
<span>Discount</span>
</label>
<select>
<option value="">
<span class="optPackage"><strong>5528</strong></span>
<span class="optM3">9m3</span>
<span class="optLWH">1.00x2.00x3.40m</span>
<span class="optPrice">€70,00</span>
<span class="optDiscount">€280,00</span>
</option>
</select>
</div>
I made something like this, but you can't split the option.
This is what I want to achieve

I was working on a solution, but have to run into a meeting. This should give you a basic idea however. Basically, you need to use JS to overlay a styled dropdown which will be linked to the actual drop down. Then you can style things to be different with in each option.
Start by creating your drop down select
You will want to make all the text as a single drop down
<select id="selectbox1">
<option value="">Select an option…</option>
<option value="basic">$100 Basic Package 3 Months</option>
<option value="upgraded">$500 Upgraded Package 6 Months</option>
<option value="expert">$1000 Expert Package 12 Months</option>
</select>
Then you theoretically, jump through hoops using Javascript
Here you will need to cache your options, then hide the select to show your styled div.
// Cache the number of options
var $this = $(this),
numberOfOptions = $(this).children('option').length;
// Hides the select element
$this.addClass('s-hidden');
// Wrap the select element in a div
$this.wrap('<div class="select"></div>');
// Insert a styled div to sit over the top of the hidden select element
$this.after('<div class="styledSelect"></div>');
// Cache the styled div
var $styledSelect = $this.next('div.styledSelect');
// Show the first select option in the styled div
$styledSelect.text($this.children('option').eq(0).text());
Now you will want to cache your styled div, and append some child elements into your div
// Cache the styled div
var $styledSelect = $this.next('div.styledSelect');
// Show the first select option in the styled div
$styledSelect.text($this.children('option').eq(0).text());
// Insert an unordered list after the styled div and also cache the list
var $list = $('<ul />', {
'class': 'options'
}).insertAfter($styledSelect);
// Insert a list item into the unordered list for each select option
for (var i = 0; i < numberOfOptions; i++) {
$('<li />', {
html: $this.children('option').eq(i).text()
.split(' ').join(' <span style="color: red; font-weight:100;">'),
rel: $this.children('option').eq(i).val()
}).appendTo($list);
}
// Cache the list items
var $listItems = $list.children('li');
You will want to use your for loop to append styling onto certain parts of your drop down. I have a basic mock up for you to check out as well.

Related

In <select> <option> element stays visible after one <option> has been clicked

this is my first question here on StackOverflow so please correct me if there is anything wrong with this question:
Three elements should show elements based on the what was chosen in the previous element.
I've created the following HTML/Twig structure
<div class="categorySelector border-two-pixel-solid" id="categorySelector" style="display:block; background: white;">
{{ _self.cpitems0(categories.categories, categories.categories_info, parts, oop_display, oop_opened) }}
<select class="categoryElement" id="typDropdown" style="display: block;">
<option class="preselect" value="" style="display: block;">--Please choose an option--</option>
{{ _self.cpitems1(categories.categories, categories.categories_info, parts, oop_display, oop_opened) }}
</select>
<select class="categoryElement" id="geraetDropdown" style="display: block;">
<option class="preselect" value="" style="display: block;">--Please choose an option--</option>
{{ _self.cpitems2(categories.categories, categories.categories_info, parts, oop_display, oop_opened) }}
</select>
</div>
This Twig macro generates the options (they are provided by a PHP controller)
enter image description here
This is the jQuery code to show/hide the , assign values to it and so on
$("#typDropdown").children().click(function(e) {
e.preventDefault();
e.stopPropagation();
console.log('before cart clear');
cart.clearcart();
console.log('after cart clear');
$curElTyp = $(this).attr("data-val-two");
console.log("currentElementValue ", $curElTyp);
$("#geraetDropdown").show();
// jQuery selector is only a string
// All child categories of clicked parent child show / hide
$("#geraetDropdown").children().not("#" + $curElTyp,).hide();
$("#geraetDropdown").children("#" + $curElTyp).show();
$("#geraetDropdown").children(".preselect").show();
//curElGeraet = $("#geraetDropdown").children("#" + $curElTyp).children('a').attr('data-val-four');
//console.log('---- curElGeraet ---- ', curElGeraet);
});
After an of a "previous" has been clicked, the in the "following" stays visible although I want the Browser to show the <option class=preselect" --Please choose an option-- to show. According to the developer console, the .preselect option is display: block; but somehow it doesn't show in the field
enter image description here
How can I update the element so that the old text disappears and the new text displays?
I've found it out
Set selected option of select box
I just have to set the value to the desired element so that the shows it!

HTML, display label if drop down has just one value

I am generating the drop down dynamically by script (Smarty).
If the drop down has just one option value, is it possible to display it as a label.
This will display a drop down with 3 values.
<select>
<option> 1 </option>
<option> 2 </option>
<option> 3 </option>
</select>
If it shows just one value then display it as label, is it possible with pure HTML or Jquery or combination of both? I could use smarty to check for the values and throw different different html, but that would make my code long as I have many drop downs.
<select>
<option> 1 </option>
</select>
Any simple logic, which I might be missing?
UPDATE (RESOLVED)
Thank for all the stackoverflow'ers who helped.
I used the code given by #ahren which worked as required.
However I have expanded the code to copy the attributes of one tag to another, in case if someone is looking for
// To replace a <select>, with <label> tag if it has just one value
$('select').each(function(){
if($(this).find('option').length === 1){
// Copy all attributes from a given tag and save it in a variable.
var attributes_from = $(this).prop("attributes");
var attributes_to = '';
$.each(attributes_from, function() {
attributes_to += ' '+this.name+'="'+this.value+'"';
});
// If select then copy its value from option.
attributes_to += ' value="'+$(this).find('option').attr('value')+'"';
// Replace the <tag>
$(this).replaceWith(function(){
return $('<label '+attributes_to+' />').html($(this).text());
});
}
});
$('select').each(function(){
if($(this).find('option').length === 1){
$(this).replaceWith(function(){
return $('<label />').html($(this).text());
});
}
});
After you've generated your dropdowns, you can just run this snippet to check each of the select elements.
DEMO: http://jsfiddle.net/kqunE/
I'd iterate over each of the <select> elements, checking the number of options they have, and make the required DOM changes accordingly:
$('select').each(function(index, select) {
var numOptions = $('option', this).length;
if(numOptions === 1) {
// replace the select element here - something like the below
var label = $('<label>').html(this.value);
$(this).after(label).hide();
}
});
I opted to hide, rather than replace, the <select> element so you still get the value sent back as part of the form. If that's not required then you can remove the element entirely using .remove() in place of .hide().

something noob about html forms [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
display dropdown values based on previous dropdown
I know html pretty well and about forms a little bit, but would like to know, for example:
when clicking on a certain drop down list item, a certain second drop down list appears based on the previous field choice. how would you go about incorporating this, is there a specific website I can go to?
an example of code would be appreciated. I am assuming that you could use javascript for this?
do you retrieve specific values or just use different drop down lists for specific choices
Thanks
of the top of my head.
You would handle your javascript on the page, before you submit your form.
step 1. reference jquery in your header
step 2. on load, hide the second select, put this script beneath you reference jquery
<script type="text/javascript">
$(function () {
$("#secondselect").hide()
$("#firstselect").change(function () {
if($(this).val() != 0){
$("#secondselect").show()
}
else
{
$("#secondselect").hide()
}
});
});
</script>
<select id="firstselect" name="firstselect" >
<option value="0">first</option>
<option value="1">second</option>
<option value="2">third</option>
<option value="3"></option>
</select>
<select id="secondselect" name="secondselect">
<option value="0">first</option>
<option value="1">second</option>
<option value="2">third</option>
<option value="3"></option>
</select>
Of the top of my head... but i'd do it something like that.
Good luck.
Oh... just a quick update.
You could use a switch instead of an if like so, might be a bit tidier...
FROM
if($(this).val() != 0){
$("#secondselect").show()
}
else
{
$("#secondselect").hide()
}
TO
switch($(this).val())
{
case '1':
$("#secondselect").show();
break;
case '1':
//do something else... show a third dropdown instead
//for instance...
// $("#thirdselect").show();
alert('got to case 1');
//or if you use firebug or chrome, right click and inspect an element then click on Console and this log report should show
console.log('got here, showing the log');
break;
default:
$("#secondselect").hide();
}
I assume from your question that you want to dynamically populate the second dropdown based on the selected value of the first one.
To do that you can use jQuery to get the value of the first selected value pass it to a PHP file to get a response of the options that the second drop down needs to have and populate them.
You can use .show() and .hide() also to hide or show the dropdown when is needed.
$('#first').change(function(){
var selected= $('#first').val();
$.getJSON('data.php',{name: selected},function(data){
var results='';
$.each(data, function(i, item) {
results += '<option value="'+item.Description+'">'+item.Description+'</option>"' ;
});
$("select#Second").html(results);
})
});
If the options do not need to dynamically change and you just need to hide and show then you can use the simpsons88 answer!

a problem with a select input in html

i hava a placed a select statement inside a table cell. The ui was looking good till certain inputs. the inputs to the dropdown are fetched dynamically from the database.certain input text are big and it can be wrapped up and that table width has increased.. is there a solution to wrap up the text after certain size or i can resrtict the size of select or can i restrict the table size to be fixed..
You can set the width of the selectbox using CSS:
<select style="width: 200px;">...</select>
This will fix the width and cut off any text that does not fit.
You can set the width of the Select with a width attribute or with styles
<select name="fd" width="230" STYLE="width: 230px" size="0">
Why not just truncate the results before they're displayed in the input? Make sure the value attribute of the option is the full text, and just truncate the internal text node of the option.
It's hard to give a more specific answer (e.g. an implementation) without knowing how the elements of the dropdown are fetched.
Or you could do it in CSS:
<select style="width:50px;">
<option value="foo">really really really really long text</option>
</select>
OK so here is the solution I figured out after quite some time. It will automatically increase the size of the select box based on the maximum width of the child options.
window.onload = function()
{
var elem = document.getElementById('test');
var values = elem.length;
var biggest_value = 0;
var biggest_option = '';
for(var i = 0; i <= values; i++)
{
if (elem.options[i])
{
var len = elem.options[i].value.length;
}
if (biggest_value < len)
{
biggest_value = len;
biggest_option = elem.options[i];
}
}
document.getElementById('test').style.width = biggest_option.offsetWidth + 'px';
};
The Select Box:
<select id="test">
<option value="Hello">Hello</option>
<option value="Hello Again">Hello Again</option>
<option value="Hello Yet Again">Hello Yet Again</option>
</select>

<select> width in IE not behaving as in other browsers

I have three select boxes.
<div style='float: left; padding-right: 10px;'>
<select size="10" name="company_id">
// a lot of options here
</select>
</div>
<div style='float: left; padding-right: 10px;'>
<select size="10" name="department_id" id="department_id">
// a lot of options here
</select>
</div>
<div style='float: left; padding-right: 10px;'>
<select size="10" name="user_id[]" id="user_id" multiple>
// a lot of options here
</select>
</div>
They are floated next to each other. When you select an item in the 1st one, an ajax query updates the values of the 2nd one.
What happens in Firefox and most other browsers is that it changes in size and pushes the 3rd one away. But in IE (6.0 and 7) the 2nd one changes size but it does not push the 3rd one away.
What i had done is to fix the size of the boxes but i want to fix this correctly, so anyone know how?
Here's the JQuery code i use to add the data to the departments select.
$.get("ajax/fetchDepartment.php?sec=departments&company_id="+company_id,
function(data){
$("#department_id").html(data);
});
data contains the <option>Stuff</option>'s needed
EDIT to add: The select boxes always have some value within them.
Here's a picture of what happens (i had to remove the items in the boxes via photoshop but you get my point)
selcet bug http://cznp.com/select_bug.jpg
IE and select options have certain 'quirks' (some, regarding select boxes and innerHTML are detailed here) about them that I've never really fully understood, but I can at least provide you with a workaround.
The trick is to add the options exlicitly to the select box, not just change the entire html of the select element wholesale. So the following works:
function changeval() {
var option = document.createElement("option");
option.text = 'my long text value to change stuff';
option.value = 'test';
$('#department_id')[0].options.add(option);
}
While this does not:
function changeval() {
var data = '<option value="test">my long test string with wide stuff</option>';
$("#department_id").html(data);
}
You might find this page helpful -apparently he fixed it by just retouching the innerHTML, so that might be a simpler option. Up to you.
The solution from the second link would look like:
function changeval() {
var data = '<option value="test">my long test string with wide stuff</option>';
$("#department_id").html(data);
$("#department_id").parent()[0].innerHTML += '';
}
try putting an option inside the selects on start. I guess u only start filling the second and third select when u selected something from the first.
I've had problems with empty selects in IE so just put an
<option value="-1">-</option>
in the 2nd and third select to start with.