(HTML) Do spans not work in select options? - html

I'm using trying to output options for products in a select dropdown, I'll just include the HTML here:
<div class="form-group">
<label for="optSel">Select Option:</label>
<select class="form-control" id="optSel">
<option selected>Default</option>
<option>Kobalt Battery:$<span id='optPrc_1'>20.00</span></option>
</select>
</div>
Actual output on web page after viewing source is the same except the spans are gone. Visually it looks the same. I need to give that price an ID as it is going to be used to calculate the total cost of a product later.
The id for the span and the value inside of it are output by a PHP script, but that's irrelevant to this problem. Am I not able to use spans in options? Are there no other tags compatible with option? If not I'll have to resort to using regex with using jQuery when I calculate the total.
I'm using a 64 bit WAMP server running PHP 7.0.4 and Apache 2.4.18. I'm also using bootsrap. My question is similar to This one but they're not the same. Thanks.

You can just use value attribute like that :
<div class="form-group">
<label for="optSel">Select Option:</label>
<select class="form-control" id="optSel">
<option selected>Default</option>
<option value="20.00" id="optPrc_1">Kobalt Battery:$20.00</option>
</select>
</div>
And you will be able to get the price later with jQuery by : $('#optPrc_1').val()

Check out this answer to a similar question:
https://stackoverflow.com/a/32661512/2084308
You can do it by using an unordered list:
<ul class="dropdown-menu">
<li>Action <span style='color:red'>like Super Man</span>
</li>
<li>Another action</li>
<li>Something else here</li>
<li role="separator" class="divider"></li>
<li>Separated link</li>
</ul>
The user uses bootstrap but if you don't want to use that, you could style the list to appear like a dropdown using custom css. Unfortunately, the option tags will not allow you to have child tags in them, per your example.

Same as Guilaume's answer, but here is a code snippet to demonstrate.
You can use the option's value attribute.
getOption = function() {
var value = 'N/A',
optNum = document.getElementById('optNumber').value,
option = document.getElementById('optPrc_' + optNum);
if(option && option.value) {
value = option.value;
}
console.log(value);
}
<div class="form-group">
<label for="optSel">Select Option:</label>
<select class="form-control" id="optSel">
<option selected>Default</option>
<option id='optPrc_1' value='20.00'>Kobalt Battery:$20.00</option>
<option id='optPrc_2' value='23.12'>Duracell Battery:$23.12</option>
<option id='optPrc_3' value='9.99'>Bobo Battery:$9.99</option>
</select>
</div>
<br />
<div>
<input type="number" min="1" max="3" value="1" id="optNumber" />
<input type="button" value="Get Option Value" onclick="getOption()" />
</div>

Related

Django An invalid form control with name='' is not focusable

I created a Django form that have dropdown in one of the field. When I tried to submit the form without selecting the dropdown item, the form is not submitted because I set in my code that it won't submit unless the form valid using is_valid().
However, it was not showing the validation error message. In the browser console, the is an error message An invalid form control with name='start_position' is not focusable.
I have read some questions about this and they said that it happened because there is a hidden field. After I inspect element, it is true that div class='start_position' is hidden.
However, I don't know how to fix it in Django.
This is the code that create dropdown field.
class LocationForm(forms.Form):
start_position = forms.ModelChoiceField(
required=True,
label="Start Position",
queryset=Location.objects.all()
)
This is the rendered HTML
<div class="formColumn form-group col-md-6 mb-0">
<div id="div_id_start_position" class="form-group">
<label for="id_start_position" class="col-form-label requiredField">
Start Position<span class="asteriskField">*</span>
</label> <div class="">
<select name="start_position" class="select form-control" required="" id="id_start_position" style="display: none;">
<option value="" selected="">---------</option>
<option value="78">Loc 78</option>
<option value="79">Loc 79</option>
<option value="80">Loc 80</option>
</select><div class="nice-select select form-control" tabindex="0">
<span class="current">Loc 78</span>
<ul class="list">
<li data-value="" class="option">---------</li>
<li data-value="78" class="option selected">Loc 78</li>
<li data-value="79" class="option">Loc 79</li>
<li data-value="80" class="option">Loc 80</li>
</ul></div>
</div>
</div>
I want the validation message show like in the other field (I tried DateField and the validation works normally).

Pass multiple parameters to url from dropdown menu

This may be a basic question but I am not able to find a solution.
I have two drop down. I want to append the url parameter from the 2nd drop down to the first and I am not able to. Here is an example.
When I select SUB11 from dropdown1 the parameter is appended in the URL like this
http://localhost:8080/test/testing.html?par1=sub11
Now when I select SUB21 from dropdown2, this is what I get (not what I want)
http://localhost:8080/test/testing.html&par2=sub21
But I want this to be like this, appended to the already existing url paramter.
http://localhost:8080/test/testing.html?par1=sub11&par2=sub21
I think it has got to do with href in the dropdown2 and I don't know what to set it to. Please help.
<ul>
<li>
Menu 1
<ul class="dropdown">
<li>SUB11</li>
<li>SUB12</li>
<li>SUB13</li>
</ul>
</li>
<li>
Menu 2
<ul class="dropdown">
<li>SUB21</li>
<li>SUB22</li>
<li>SUB23</li>
</ul>
</li>
</ul>
Ideally you could create a HTML form and use <select> element.
<form method="get">
<select name="menu-1">
<option value="SUB11">SUB11</option>
<option value="SUB12">SUB12</option>
<option value="SUB13">SUB13</option>
</select>
<select name="menu-2">
<option value="SUB21">SUB21</option>
<option value="SUB22">SUB22</option>
<option value="SUB23">SUB23</option>
</select>
<input type="submit" value="Submit" />
click on submit and you will see they are in the URL:
http://localhost:8080/test/testing.html?menu-1=SUB11&menu-2=SUB21
Check this out https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select
Use Jquery
$( '.dropdown' ).on( 'change', function( e ){
document.location.href = "localhost:8080/test/testing.html?par1=" + "&par2=" + $( this ).val() + "seconddropdownvalue";
});
Give id to second drop-down to get value using jquery.

How to make any area visible and hidden by Knockoutjs based on checkbox chekced

i like to use Knockoutjs in page where a particular <ul> portion will be visible if a specific check box is checked. if check the <ul> portion will be visible and if unchecked then <ul> portion will be invisible. i searched google and found people do this way which is not same to my requirement.
<ul data-bind="foreach: tasks, visible: tasks().length > 0">
<li>
<input type="checkbox" data-bind="checked: isDone" />
<input data-bind="value: title, disable: isDone" />
Delete
</li>
</ul>
they are using this trick data-bind="foreach: tasks, visible: tasks().length > 0"
they are checking if view model has more task then it will be visible or not but in my case i need to use checkbox. anyone can tell me how to achieve it. thanks
You will need another observable binded to the checkbox, in javascript:
var SimpleListModel = function(items) {
this.isListVisible = ko.observable(true);
this.items = ko.observableArray(items);
};
and in html:
<input type="checkbox" data-bind="checked: isListVisible" > Show list
<p>Your items:</p>
<ul width="50" data-bind="foreach: items, visible: isListVisible">
<li data-bind="text: $data"></li>
</ul>
See jsFiddle

Flat-ui select dropdown is not functioning

I am using Flat-UI (http://designmodo.github.io/Flat-UI/) to aid with the front-end elements of a small MeteorJS App I am building.
The problem I am having is that the Select Dropdown is not working. And I would like to know how to get the option value.
Please see the below Markup:
<div class="select2-container form-control select select-primary" id="s2id_autogen1">
<a href="javascript:void(0)" class="select2-choice" tabindex="-1">
<span class="select2-chosen" id="select2-chosen-2">My Profile</span>
<abbr class="select2-search-choice-close"></abbr>
<span class="select2-arrow" role="presentation">
<b role="presentation"></b>
</span>
</a>
<label for="s2id_autogen2" class="select2-offscreen"></label>
<input class="select2-focusser select2-offscreen" type="text" aria-haspopup="true" role="button" aria-labelledby="select2-chosen-2" id="s2id_autogen2">
</div>
<select class="form-control select select-primary" data-toggle="select" tabindex="-1" title="" style="display: none;">
<optgroup label="Profile">
<option value="0">My Profile</option>
<option value="1">My Friends</option>
</optgroup>
<optgroup label="System">
<option value="2">Messages</option>
<option value="3">My Settings</option>
<option value="4">Logout</option>
</optgroup>
</select>
This is what we have in flatui document. But the select dropdown is not functioning, can someone help me to make it work and pull value from the options?
We have made a custom select due to the inability of styling the system one. It is based on the Select2 plug-in with the source slightly modified to meet Bootstrap naming convention.
Select2 3.5.2
Check this:
http://select2.github.io/select2/
$("#selectList")
.on("change", function(e) { log("change "+JSON.stringify({val:e.val, added:e.added, removed:e.removed})); })
If you read the documentation well you will notice you have manually add a select2 call to transform all selects.
E.g. like this:
$( document ).ready(function() {
$("select").select2({dropdownCssClass: 'dropdown-inverse'});
});

CSS to select another Element based on a HTML Select Option Value

Is there a CSS selector that allows me to select an element based on an HTML select option value?
<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<p>Display only if an option with value 1 is selected</p>
I'm looking for an HTML/CSS only method to only display a selected number of form fields. I already know how to do it with Javascript.
Is there a way to do this?
Edit:
The question title is perhaps misleading. I'm not trying to style the select box, that's pretty common and plenty of answers on SO already. I'm was actually trying to style the <P> element based on the value selected in the <select>.
How ever what I'm really trying to do is to display a number of form fields based on a selected numeric value:
<select name="number-of-stuffs">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<div class="stuff-1">
<input type="text" name="stuff-1-detail">
<input type="text" name="stuff-1-detail2">
</div>
<div class="stuff-2" style="display:none">
<input type="text" name="stuff-2-detail">
<input type="text" name="stuff-2-detail2">
</div>
<div class="stuff-3" style="display:none">
<input type="text" name="stuff-3-detail">
<input type="text" name="stuff-4-detail2">
</div>
I would like to display div.stuff-1 and div.stuff-2 when number-of-stuffs=2 and display div.stuff-1 div.stuff-2 and div.stuff-3 when number of stuffs=2.
Something like this fiddle
Its called an attribute selector
option[value="1"] {
background-color:yellow;
}
Example http://jsfiddle.net/JchE5/
You can use
select option[value="1"]
but browser support won't be fantastic.
This probably requires a parent selector which has not been specified for CSS2 or CSS3.
CSS selector for "foo that contains bar"?
Is there a CSS parent selector?
A subject selector has been defined in the CSS4 working draft as of May 2013 but no browser vendor has implemented it yet.
Whether the method in question works (in theory) using the subject selector remains to be seen.
How about the alternative below?
You don't get a select box, but perhaps this is close enough.
#option-1,
#option-2,
#option-3 {
display: none;
}
#nos-1:checked ~ #option-1,
#nos-2:checked ~ #option-2,
#nos-3:checked ~ #option-3 {
display: block;
}
<input id="nos-1" name="number-of-stuffs" type="radio" value="1" checked="checked" /><label for="nos-1">1</label>
<input id="nos-2" name="number-of-stuffs" type="radio" value="2" /><label for="nos-2">2</label>
<input id="nos-3" name="number-of-stuffs" type="radio" value="3" /><label for="nos-3">3</label>
<div id="option-1">
<input type="text" name="stuff-1-detail" value="1-1" />
<input type="text" name="stuff-1-detail2" value="1-2" />
</div>
<div id="option-2">
<input type="text" name="stuff-2-detail" value="2-1" />
<input type="text" name="stuff-2-detail2" value="2-2" />
</div>
<div id="option-3">
<input type="text" name="stuff-3-detail" value="3-1" />
<input type="text" name="stuff-4-detail2" value="3-2" />
</div>
i believe that in "live" or realtime, it is possible only with javascript or jQuery. Wrap the potentially hidden fields in divs with display: none onLoad and have JS or jQuery change state to display: block or however you like.
//Show Hide based on selection form Returns
$(document).ready(function(){
//If Mobile is selected
$("#type").change(function(){
if($(this).val() == 'Movil'){
$("#imeiHide").slideDown("fast"); // Slide down fast
} else{
$("#imeiHide").slideUp("fast"); //Slide Up Fast
}
});
//If repairing is selected
$("#type").change(function(){
if($(this).val() == 'repairing'){
$("#problemHide").slideDown("fast"); // Slide down fast
$("#imeiHide").slideDown("fast"); // Slide down fast
}else{
$("#problemHide").slideUp("fast"); //Slide Up Fast
}
});
});
// type is id of <select>
// Movil is option 1
// Repairing is option 2
//problemHide is div hiding field where we write problem
//imeiHide is div hiding field where we write IMEI
i am using in one of my apps...
Possible with PHP too, but with PHP, you will have to send the change request or you can write a code in php to have certain classes to be loaded based on already selected values, for example
<select class="<?php if($valueOne == 'Something'){echo 'class1';}else{echo 'class2';}">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
I found a solution based on this answer
satisfies op's request most. Not purely in CSS but definitely least amount of JavaScript is involved.
select[data-chosen='1']~.stuff-1{
display: block !important;
}
select:not([data-chosen='1'])~.stuff-1{
display: none;
}
select[data-chosen='2']~.stuff-2{
display: block !important;
}
select[data-chosen='3']~.stuff-3{
display: block !important;
}
<select name="number-of-stuffs" data-chosen = "1" onchange = "this.dataset.chosen = this.value;">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<div class="stuff-1">
<span> I am stuff-1!</span>
<input type="text" name="stuff-1-detail">
<input type="text" name="stuff-1-detail2">
</div>
<div class="stuff-2" style="display:none">
<span> I am stuff-2!</span>
<input type="text" name="stuff-2-detail">
<input type="text" name="stuff-2-detail2">
</div>
<div class="stuff-3" style="display:none">
<span> I am stuff-3!</span>
<input type="text" name="stuff-3-detail">
<input type="text" name="stuff-4-detail2">
</div>
You don't even need to write any separated js, despite embedding "this.dataset.chosen = this.value;" in onchange seems to be a bit hacky.