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

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.

Related

Hide HTML elements based on input="radio" w CSS only

I have labels and select lists associated with 2 different classes. Based on the radio button checked I would like to hide the elements associated to one class and show the elements associated to the other class.
HTML
Radio Button
<p>Your Preferred Input</p>
<label for="qual_quant">Qualitatively</label>
<input type=radio id="rb_qual" name="qual_quant" value="qual">
<label for="qual_quant">Quantitatively</label>
<input type=radio id="rb_quant" name="qual_quant" value="quant" checked>
This is an example of the code to be shown when #rb_qual = "qual"
<label for="fs_qual_tech" class="fs_qual">Technical - Qual</label>
<select id="fs_qual_tech" name="fs_qual_tech" class="fs_qual">
<option value="" disabled selected hidden>Choose Trait...</option>
<option value="2.5">Very Poor</option>
<option value="8">Poor</option>
<option value="13">Good</option>
<option value="18">Very Good</option>
</select>
Below is an example of code to be hidden when #rb_qual="qual"
<div class="fs_quant_div">
<label for="fs_quant_tech" class="fs_quant">Technical - Quant</label>
<select id="fs_quant_tech" name="fs_quant_tech" class="fs_quant">
<option value="" disabled selected hidden>Assign Points...</option>
<option value="1">1</option>
<option value="2">2</option>
</select>
CSS
condition which hides all .fs_qual elements
.fs_qual {
display: none;
}
Trigger which shows .fs_qual
#rb_qual:checked ~ .fs_qual {
display: block;
}
Everything works -- except that trigger. If someone can help with this trigger I can expand on it for the other class and radio button value.
Screenshot of how it looks / functions(not)
I was able to make the trigger work for both cases.
You have added the quants inside the div.
So, the selector #rb_quant:checked ~ .fs_quant would not work as expected.
Please check this fiddle and let me know.
https://jsfiddle.net/xqekr762/

Why do the data-price values of my alternative select options not get summed when performing this function?

BACKGROUND
I am trying to get the values of these inputs totalled up, dependent upon which radio button is selected at the beginning. Each radio button gives a number of different select inputs, each with their own data-price value.
When a radio button is selected, the divs for the other radio buttons disappear, leaving only the corresponding radio button div.
There are also some checkboxes which can be selected, but a helpful commenter already assisted me with those.
THE PROBLEM
The selected radio button and it's and visible div include the select input type, and this is where i am having issues.
I can't get the option data-price values to sum when selecting anything other than the first radio button selection and it's corresponding select input.
For example, when selecting radio button DJ and one of it's select options, only the data-price of the radio button is shown.
I need the select option to be added to the chosen radio button, and also for the sum to return to zero whenever a different radio button is clicked.
I'm finding this part really very tricky, any pointers in the right direction would be truly wonderful.
Thank you.
HTML
<div class="wrapper" id="go">
<h3>I want a :</h3>
<label><input type="radio" name="colorRadio" value="one" data-price="1600">Live Band</label>
<label><input type="radio" name="colorRadio" value="two" data-price="750">DJ</label>
<label><input type="radio" name="colorRadio" value="three" data-price="700">Acoustic</label>
<label><input type="radio" name="colorRadio" value="four" data-price="350">Classicals</label>
<label><input type="radio" name="colorRadio" value="five" data-price="800">Indigenous Performance</label>
<label><input type="radio" name="colorRadio" value="six" data-price="1200">World Music Band</label>
</div>
<br>
<br>
<div class="box one"><h2>What’s included with a live band From $1600</h2>
<select name="band-options" id="live-band-options" class="type">
<option value="">-Please choose one of these options-</option>
<option value="guitar-bass-drums" data-price="0">3-Guitar, Bass/Double Bass and Drums</option>
<option value="singer" data-price="0">3- Singer and Guitar/Keys and Drums/Double Bass </option>
<option value="add-sax-or-singer" data-price="400">4-Add Saxophone/Singer (+$400)</option>
<option value="add-sax-and-singer" data-price="800">5-Add Saxophone and Singer (+$800)</option>
</select>
</div>
<div class="box two"><h2>What’s included with our party magic DJ From $750 </h2>
<label for="dj-options"></label><select name="dj" id="dj-select" class="type">
<option value="">-Please choose one of these options</option>
<option value="DJ" data-price="0">DJ</option>
<option value="sax-or-string" data-price="500">2-Sax or Strings (+$500)</option>
<option value="sax-and-string" data-price="1000">3-Sax and Strings(+$1000)</option>
</select>
</div>
<div class="box three"><h2>What's included with our Acoustic performances From $700</h2>
<label for="acoustic-options"></label><select name="acoustic" id="acoustic-select" class="type">
<option value="">-Please choose one of these options-</option>
<option value="solo-guitar-or-singer" data-price="0">Solo Guitar/Singer</option>
<option value="solo-piano-or-singer" data-price="0">Solo Piano/Singer</option>
<option value="acoustic-duo" data-price="500">Duo(+$500)</option>
</select>
</div>
<div id="checker">
<div>
<input type="checkbox" name="add-ons" id="add-dj" data-price="750">
<label for="scales">Add a DJ ($750) Mix it up with a DJ to see out the evening</label>
</div>
<div>
<input type="checkbox" name="add-ons" id="add-acoustic-wedding-music" data-price="450">
<label for="add-dj">Add Acousitc Weddding Music ($450) Add a delightful singer and guitarist to your ceremony</label>
</div>
</div>
<div id="result" class="container"></div>
CSS
.wrapper input[type="radio"] {
opacity:0;
position:fixed;
width:0;
}
.wrapper label{
display:inline-block;
background-color:#ddd;
padding:10px 20px;
font-family:sans-serif, Arial;
font-size:16px;
border:2px solid #444;
border-radius:4px;
}
.wrapper label input[type="radio"]:checked + {
background-color:black;
border-color: green;
opacity:1;
}
Jquery
<script type="text/javascript">
$(document).ready(function(){
$('input[type="radio"]').click (function(){
var x = $(this).attr("value");
var y =$("."+ x);
$(".box").not(x).hide();
$(y).show();
});
});
$(document).ready(function(){
function validate() {
var sum = 0;
sum += +$('#go input:checked').data('price') || 0;
sum += +$('.type option:selected').data('price') || 0;
$('#checker input:checked').each(function(){
sum += +$(this).data('price')
})
$('#result').html(sum === 0 ? '' : sum + '$');
}
validate();
$('#go input, .type, #checker input').on('change', function() {
validate();
});
});
</script>
Your problem is that you're not clearing selections when you switch the top-level radio buttons, so you're leaving selected items from the prior set of selections.
Note that .hide() does NOT remove the items from the DOM. It toggles the visibility, but the elements are still present. And if they're selected, they will be tallied (if not cleared).
Respectfully, your code and UI need substantial work to be readable (it's very difficult to understand how this should even behave). But I think the key is this when you select a new top-level item:
$('.type option:selected').removeAttr("selected"); //clear dropdowns
You can see some mods that I made to your code here:
https://jsfiddle.net/012hpq6e/

How do I control the tabbing order of input tags in html?

I have the following html input fields, two are for user input one to submit the responses.
<div>
<select id="netcall" class="" name="netcall" onchange="chngDFLTS()">
<option value="0" selected>Select One</option>
<option value="1" selected>Select Two</option>
</select>
</div>
<div>
<input id="cs1" type="text" value="" tabindex=1 />
<input id="Fname" type="text" value="" tabindex=2 />
<input id="ckin1" type="submit" value="Check In" tabindex=3 />
</div>
If I understand http://www.w3schools.com/tags/att_global_tabindex.asp correctly then the tab sequence should be 1 to 2 to 3. Tabbing from CS1 to Fname works every time, but the next tab takes me to a select field which does not even have a tabindex value and is not even in the same div tag. Additionally no amount of further tabbing ever takes me to the ckin1, submit input tag.
What am I not understanding?

"checked" and "selected" in HTML for input do not work with AngularJS

I am learning AngularJS and user input. In my code, I tried to set default state for drop down menu and radio button with "selected" and "checked".
However, they do not work together with "ng-model" attribute.
Also, for the first radio button (ascending), the empty value attribute seems to hinder with "checked" attribute.
Could anyone explain why this happens and how to bypass this problem?
<div class="search">
<h1>Artist Directory</h1>
<label> Search: </label>
<input ng-model="query" placeholder="Search for artists" autofocus>
<label class="formgroup">by:
<select ng-model="listOrder" name="direction">
<option value ="name" selected="selected"> Name</option>
<option value="reknown"> Reknown</option>
</select>
</label>
<label class="formgroup">
<input ng-model="direction" type="radio" value=" " checked> Ascending
</label>
<label class="formgroup">
<input ng-model="direction" type="radio" value="reverse"> Descending
</label>
</div>
tie your button and select to an ng-model. Default values are the values you put in the model when the scope is first created. Then values are updated when user clicks :
function test($scope) {
$scope.selected = ['1','3'];
$scope.checked = "green";
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js"></script>
<div ng-app>
<h2>Todo</h2>
<div ng-controller="test">
<select name="" ng-model="selected" multiple>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<br><br>
<input type="radio" ng-model="checked" value="red"> Red <br/>
<input type="radio" ng-model="checked" value="green"> Green <br/>
<input type="radio" ng-model="checked" value="blue"> Blue <br/>
<br>
Selected : {{selected}}
<br>
Checked : {{checked}}
</div>
</div>
See it in action : http://jsfiddle.net/913n30zf/
Select in AngularJS looks likes this:
<select ng-model="selectedOpt"
ng-options="opt for opt in listOrder">
</select>
And back in the controller you set
$scope.selectedOpt = $scope.listOrder[0];
which option you want to be select by default.

textarea's "required" attribute doesn't work even though the value is empty

I created a simple page with list box and text area with conditions that all should be required.
List box are working fine, but textarea box doesn't tell that the field is required to be filled.
<!DOCTYPE html>
<html>
<head>
<title>Ratings & Reviews</title>
<body>
<form>
<span>Customer Service*</span>
<span>
<select name=customer id=aa required>
<option selected="rate" value="" disabled>rate</option>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
</select>
</span>
<br><br>
<span>Value for money*</span>
<span>
<select name=money id=aa required>
<option selected="rate" value="" disabled>rate</option>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
</select>
</span>
<div>
<textarea name="reviews" rows=11 cols=50 maxlength=250 required>
</textarea>
</div>
<div id=submit>
<br>
<input type=submit name=submit value=submit>
</div>
</form>
</body>
</html>
You have empty space inside text area, remove it:
<textarea name="reviews" rows=11 cols=50 maxlength=250 required ></textarea>
Fiddle demo
The problem is with the spaces between the tags. You are not supposed to give any spaces in html between these tags, otherwise browser will consider it as the value.
try this
<textarea name="mm" id="mm" rows="5" placeholder="NA if not applicable" required="required"></textarea>
And probaly form has novalidate attribute. Any form-element validation attributes (like required or regexp) with form novalidate attribute will ignore.
I faced similar problem. I left space between opening and closing tag of Textarea as in following code
<label><b>Register As:*</b></label>
<select name="category" id="category" class="form-control"
onchange="toggleInput()" required>
<option value=''>--Select--</option>
<option value='Attendee'>Attendee</option>
<option value='Presenter'>Presenter</option>
</select>
...
<textarea name="description" id="description" class="form-control"
placeholder="Explain here what you will present..."> </textarea>
and in my javascript I was trying following
<script>
function toggleInput() {
if( document.getElementById("category").value=="Attendee"){
document.getElementById("description").required = false;
}
else{
document.getElementById("description").required = true;
}
}//End Function
</script>
And I could not figure out what was the problem until I landed on this page. It was the space. Thanks #sinisake for the solution. Hope sharing my experience would help someone
Don't use spaces between opening and closing tags.
e.g:
<textarea required>**Don't put any space here**</textarea>
It will work fine.
Check default value in the textarea. There must be blank spaces which are considered as value.