We have built an html form which contains basic stuff with input and option fields. This works good!
Then we have some fields to choose an option:
<h2 class="space">Franchise</h2>
<div class="spa2">
<p option value="300" data-value="300" class="s2">300</p>
<p option value="500" data-value="500" class="s2 selected">500</p>
<p option value="1000" data-value="1000" class="s2">1000 </p>
<p option value="1500" data-value="1500" class="s2">1500</p>
<p option value="2000" data-value="2000" class="s2">2000 </p>
<p option value="2500" data-value="2500" class="s2">2500</p>
</div>
This displays buttons are to choose specific values, but respective data is not transferred with POST because there are some non-html form type fields.
Is there a way to cheat and tell that this are form fields without change anything?
The code displays this buttons here:
Change them into <form> elements
You're asking the wrong question. This look can be achieved with form elements, which is easier and more reliable than "cheating".
HTML
<form>
<input id="one" type="radio" name="choices" />
<label for="one">300</label>
<input id="two" type="radio" name="choices" />
<label for="two">500</label>
<input id="three" type="radio" name="choices" />
<label for="three">1000</label>
</form>
This example consists of radio inputs. Use this if there is only one choice, otherwise make the type checkbox if you can have multiple choices.
The label should follow the input so that the corresponding label can be styled when the input is checked.
The label is connected to its corresponding radio input with the matching for and id attributes.
The radio input options are linked with the same name="choices" and only one option can be selected.
CSS
input[type=radio] {
display: none;
}
input[type=radio] + label {
padding: 10px;
border: solid 1px;
cursor: pointer;
}
input[type=radio]:checked + label {
background: red;
}
Hide the radio buttons with display: none
Style the labels that follow the radio buttons input[type=radio] + label
Change the styles when a radio button is selected with input[type=radio]:checked + label
Make the cursor a pointer with cursor: pointer on the labels
Full Example
form {
margin: 20px;
}
input[type=radio] {
display: none;
}
input[type=radio]+label {
padding: 10px;
border: solid 1px;
cursor: pointer;
}
input[type=radio]:checked+label {
background: red;
}
<form>
<input id="one" type="radio" name="choices" />
<label for="one">300</label>
<input id="two" type="radio" name="choices" />
<label for="two">500</label>
<input id="three" type="radio" name="choices" />
<label for="three">1000</label>
</form>
Related
I made a label because I wanted to apply css to the checkbox.
However, the label is not working except for the checkbox whose id is 'checkall'.
<div class="divCenter cartDiv">
<ul>
<li class="selectallforcart">
<div class="hanadiv">
<input type="checkbox" id="checkall" class="check" value="0"/>
<label for="checkall"></label> selelct All
</div>
</li>
</ul>
<% ArrayList<LikePdVO> likeList = (ArrayList<LikePdVO>)(request.getAttribute("likeList"));
for(LikePdVO vo : likeList) {
String arr[] = vo.getPhoto().split("\\*");
%>
<div class="cartProduct" id="<%=vo.getPd_id()%>">
<div class="fl cartCheck">
<input type="checkbox" class="check" name="<%=vo.getName()%>"
value="<%=vo.getOrder_price()%>" id="<%=vo.getPd_id()%>">
<label for="<%=vo.getPd_id()%>">
</label>
</div>
this is my code.
.cartDiv input[type=checkbox] + label {
display: inline-block;
width: 20px;
height: 20px;
border: 2px solid #bcbcbc;
cursor: pointer;
}
.cartDiv input[type=checkbox]:checked + label {
background-color: #866744;
}
.cartDiv input[type=checkbox] {
display: none;
}
And this is CSS.
When you mean that the label is not working, I assume you mean that clicking on it doesn't cause the checkbox to be checked?
Either way, the label's text needs to be within the element, so it should be:
<label for="checkall">Select all</label>
And your other labels contain no text, so the label itself is probably quite small, despite the CSS, and therefore not clickable.
You need to set label text in the label tag not out side of it.
<div class="divCenter cartDiv">
<ul>
<li class="selectallforcart">
<div class="hanadiv">
<input type="checkbox" id="checkall" class="check" value="0" />
<label for="checkall">selelct All</label>
</div>
</li>
</ul>
</div>
I have this working for a checkbox. When I select the checkbox, my two radio buttons appear. But now, if I select one of the radio buttons, I want a block of address fields to appear. I'm not sure why the same thing that is working for the checkbox isn't working for the radio button.
<form>
<input class="no22XAddressRadioButtons_activator" type="checkbox">
Check if you do not have a physical address within the Oakland Beat 22X boundary.
<div class="no22XAddressRadioButtons">
<input class="outsideAddress_activator" type="radio" name="no22XAddress" id="outsideAddress_activator">
<label for="outsideAddress_activator">My physical address is outside of the 22X boundary.</label><br>
<input class="noAddress" type="radio" name="no22XAddress" id="no22XAddress">
<label for="no22XAddress">I don't have a physical address.</label><br>
</div>
<!--Begin conditional address-->
<div class="outsideAddressTextFields">
Enter your physical address including city, state, and zip code.
<input class="outsideAddress" type="text">
<label class="labelText" for="outsideAddress">Number and Street</label>
<input type="text" name="outsideAddress" id="outsideAddress" required>
</div>
</form>
/* Begin conditional radio buttons */
.no22XAddressRadioButtons {
display: none;
}
.no22XAddressRadioButtons_activator {
margin-left: 30px;
}
.no22XAddressRadioButtons_activator:checked + .no22XAddressRadioButtons {
display: block;
margin-left: 45px;
}
/* Begin conditional address */
.outsideAddressTextFields{
display: none;
}
.outsideAddress_activator:checked + .outsideAddressTextFields {
display: block;
}
Thats because you're using +, an Adjacent sibling combinator. Which means the block that you need to be made visible, while an input is checked has to be right after the said input. You can use a General sibling combinator which I think might be helpful in you case. I've attached a modified code snippet to demonstrate.
As a side note: use labels to associate an input to a caption.
You can click the associated label to focus/activate the input, as well as the input itself. This increased hit area provides an advantage to anyone trying to activate the input, including those using a touch-screen device.
/* Begin conditional radio buttons */
.no22XAddressRadioButtons {
display: none;
}
.no22XAddressRadioButtons_activator {
margin-left: 30px;
}
.no22XAddressRadioButtons_activator:checked ~ .no22XAddressRadioButtons {
display: block;
margin-left: 45px;
}
/* Begin conditional address */
.outsideAddressTextFields{
display: none;
}
.outsideAddress_activator:checked ~ .outsideAddressTextFields {
display: block;
}
<form>
<input id="addressCheck" class="no22XAddressRadioButtons_activator" type="checkbox">
<label for="addressCheck">Check if you do not have a physical address within the Oakland Beat 22X boundary.</label>
<div class="no22XAddressRadioButtons">
<div>
<input class="outsideAddress_activator" type="radio" name="no22XAddress" id="outsideAddress_activator">
<label for="outsideAddress_activator">My physical address is outside of the 22X boundary.</label>
<!--Begin conditional address-->
<div class="outsideAddressTextFields">
<div>
<label for="insideAddress">Enter your physical address including city, state, and zip code.</label>
<input class="outsideAddress" type="text" id="insideAddress">
</div>
<div>
<label class="labelText" for="outsideAddress">Number and Street</label>
<input type="text" name="outsideAddress" id="outsideAddress" required>
</div>
</div>
</div>
<div>
<input class="noAddress" type="radio" name="no22XAddress" id="no22XAddress">
<label for="no22XAddress">I don't have a physical address.</label><br>
</div>
</div>
</form>
I would like to have a form validation with Just HTML5 with Submit button style changes depends on the all the input validation values. ie to display Color of submit button in green if all the input fields are valid /Form is valid .
You can style the submit button based on the form's :valid or :invalid pseudo-class:
form:valid input[type="submit"] {
background-color: #cfc;
color: #060;
}
form:invalid input[type="submit"] {
background-color: #fcc;
color: #600;
}
label {
display: block;
margin: 0.5em 0;
}
<form>
<label>
Serial number:
<input pattern="[A-Za-z]{3}[0-9]{3}" required placeholder="Something like ABC123">
</label>
<label>
<input type="radio" name="radio" value="1" required>
One of these
</label>
<label>
<input type="radio" name="radio" value="2" required>
must be selected
</label>
<input type="submit">
</form>
The only validation that can be done with just HTML is to use the HTML5 'required' attribute which doesn't let a form to be submitted before you do something (click a radio button, fill a text field...).
You can't validate a form to your own likes, and you certainly can't dynamically change elements' CSS properties without JavaScript.
HTML has a design problem, in my opinion, whereby form input elements are inconsistently a type of <input /> (checkbox, text, etc) or are their own element type (select, textarea, etc). I understand it's based on the element's needs for child elements or not (<select> and <textarea> have children, but <input /> does not) but the different types of <input /> result in wildly different rendering - for example, checkbox and radio generally render as a small clickable square-shared input, whereas text and password are rectangular with no prescribed width.
This means that if you're styling inputs, you will need multiple rules.
Given this HTML:
<div class="field">
<label>
<input type="checkbox" /> Option 1
</label>
<label>
<input type="checkbox" /> Option 2
</label>
</div>
<div class="field">
A question?
<label>
<input type="radio" /> Option 1
</label>
<label>
<input type="radio" /> Option 2
</label>
</div>
<div class="field">
<label>
Your name
<input type="text" />
</label>
</div>
<div class="field">
<label>
An essay
<textarea></textarea>
</label>
</div>
If you want to style only text inputs, you might think you would only need:
input,
textarea {
border: 1px inset green;
}
However this will also (inappropriately) style the radio and checkbox inputs, so you either need to style input and then add a new rule for input[type=radio] and input[type=checkbox] that either re-styles them to match your design or resets their style - or change the rule to match input[type=text].
But neither is perfect, because the input rule still doesn't specify a style for the other types of input: either those modelled after a textbox (e.g. password and email) or those that are not (color, image, file, etc). You will need to add a rule for each and every type that you believe you could need - in addition to all of the other form elements, like select, textarea, button, and so on - and then you will need to repeat these selectors for each case the style needs to be different in a new context:
input[type=text],
input[type=password],
input[type=search],
input[type=tel],
input[type=url],
input[type=email],
textarea {
border: 1px inset green;
}
input[type=radio],
input[type=checkbox] {
border: none;
}
.someWrapper input[type=text],
.someWrapper input[type=password],
.someWrapper input[type=search],
.someWrapper input[type=tel],
.someWrapper input[type=url],
.someWrapper input[type=email],
.someWrapper textarea {
border: 1px inset green;
}
.someWrapper input[type=radio],
.someWrapper input[type=checkbox] {
border: none;
}
/* etc */
However I feel many inputs can be grouped into "input classes", such as the "text input" class: text, password, textarea, email, search, etc - the "box input" class: checkbox, radio, the "date" class: date, datetime, month, etc, and so on.
So rather than manually adding these classes as class values to my inputs in HTML, is there any browser-intrinsic way, such as through a CSS pseudo-class, e.g. input:textbox or input:boxinput? If so, this would greatly simplify CSS selectors for forms and reduce CSS bugs from missing selectors.
Luckily, there is something in CSS that nearly does what you want. The :read-write psuedo-selector (https://developer.mozilla.org/en-US/docs/Web/CSS/:read-write) selects elements that are editable by the user.
Consider the following:
<input type="radio" selected /><br/>
<input type="checkbox" selected><br>
<input type="button" value="Click me"><br>
<input type="submit" value="Submit me"><br>
<input type="text"><br>
<input type="password"><br>
<input type="email"><br>
<input type="date"><br>
<textarea></textarea><br>
The bottom 5 elements (not counting brs) will be selected and highlighted with the following one line of CSS:
*:read-write {border:1px solid #f3f;}
Browser support is fairly good for basic form fields.
Note: I say nearly in the first line. This selects date fields.
You should be able to just use input[type] however, that does not necessarily help you when applying to select or textarea fields. But you would have to add those selectors regardless.
input[type],
textarea {
border: 1px inset green;
}
input[type=radio],
input[type=checkbox] {
border: none;
}
input[type],
textarea {
border: 1px inset green;
}
input[type=radio],
input[type=checkbox] {
border: none;
}
<input type="text"/><br/>
<input type="button" value="click"/><br/>
<input type="checkbox"/><br/>
<input type="radio"/><br/>
<input type="color"/><br/>
<textarea></textarea>
in following jsfiddle, I am trying to change the color of selected radio button for two (or more) different radio groups, by only using CSS. I am missing something i do not know.
http://jsfiddle.net/2nxnk/1/
CODE:
<input type="radio" name="group0" value="1" /> One
<input type="radio" name="group0" value="2" /> Two
<input type="radio" name="group0" value="3" /> Three
<p>
<input type="radio" name="group1" value="4" /> Four
<input type="radio" name="group1" value="5" /> Five
<input type="radio" name="group1" value="6" /> Six
CSS:
input[type="radio"]:checked{ color: green; }
i do not want to use any jquery unless there is no other way. pl advice.
EDIT:
I decided to edit the question: I want to change the color of 'text of radio button'. secondly i did not want to clutter the html page with ID's if the only use is to change text color. My question now is:
1. is using the label only way?
2. seems using IDs you get capability of coloring different radio group (text again) differently. is that correct?
3. finally maybe a simple question: I though only javascript catches the action on the page, when there is no javascript how does CSS triggers the effect?
thx all for help and I also upped the last example which colors the radio button image itself.
If you want to change the text next to the radio button, you can do this way
Html
<input type="radio" name="group0" value="1" /><label>One</label>
<input type="radio" name="group0" value="2" /><label>Two</label>
<input type="radio" name="group0" value="3" /><label>Three</label>
<p>
<input type="radio" name="group1" value="4" /><label>Four</label>
<input type="radio" name="group1" value="5" /><label>Five</label>
<input type="radio" name="group1" value="6" /><label>Six</label>
Css
input[type="radio"]:checked + label {
color: green;
}
Fiddle
Probably pseudo element can help a bit:-
input[type="radio"]:checked +label {
color: green;
}
input[type="radio"]:checked:before
{
content:'.';
position:relative;
display:inline-block;
background-color:green;
width:12px;
opacity:.3;
top:-1px;
border-radius:10px;
-moz-border-radius:10px;
}
Fiddle
In most (if not all) browsers, you cannot change the colour of a radio button at all.
You can only simulate it by replacing the radio button with a different widget and trying to link it to the real radio button so it continues to work. Most implementations of this depend on JavaScript.
If you are looking to change the color of the text if respective radio button is selected, you can use + adjacent selector and wrap the text inside a label tag
input[type="radio"]:checked + label {
color: green;
}
Demo
I've removed other boxes to decrease the clutter, but logic goes same for else
Did you consider making your own radio boxes? Using labels that is
We can make labels act like radio boxes by using the "for" attribute, which then makes it clickable and react to the :checked selector. The best thing about these is a label is a basic HTML element so you can make it look exactly as you intend. Check this relatively plain example:
DEMO
<input type="radio" name="group0" value="1" id="one"/>
<label for="one"><div class="inner"></div></label><span>One</span>
<input type="radio" name="group0" value="2" id="two"/>
<label for="two"><div class="inner"></div></label><span>Two</span>
input {
display: none;
}
span,
label {
display: block;
margin: 10px 10px 10px 0;
float: left;
}
label {
width: 20px;
height: 20px;
background: #ddd;
border-radius: 10px;
}
input:checked + label{
background: green;
}
.inner {
width: 10px;
height: 10px;
margin-top: 4px;
margin-left: 4px;
background: #eee;
border-radius: 5px;
border: 1px solid #aaa;
}
Update
Here is a new fiddle with the actual buttons like I was referring to. Link: jsfiddle
Others have answered how to change the text, which I believe is what you are looking for based on your attempt. But, in the off chance you are looking to change the appearance of the button itself you could use <label for="#id"> and some CSS styling/opacity tricks to replace the buttons with images. Then, change the background color when checked. My example and fiddle use placekittens, but you could substitute the <img> sources with any image you wanted (a colored radio button with 50% opacity for example would allow you to disregard the opacity section of code.) See my code below and this fiddle. You'll have to play with the margin and padding settings to make the background color canvas the entire image, but this should give you an idea.
HTML
<input id="a" type="radio" name="group0" value="1" />
<label for="a"><img src="http://www.placekitten.com/40/40" /></label>
<input id="b" type="radio" name="group0" value="2" />
<label for="b"><img src="http://www.placekitten.com/40/39" /></label>
<input id="c" type="radio" name="group0" value="3" />
<label for="c"><img src="http://www.placekitten.com/39/40" /></label>
<p>
<input id="d" type="radio" name="group1" value="4" />
<label for="d"><img src="http://www.placekitten.com/38/40" /></label>
<input id="e" type="radio" name="group1" value="5" />
<label for="e"><img src="http://www.placekitten.com/40/38" /></label>
<input id="f" type="radio" name="group1" value="6" />
<label for="f"><img src="http://www.placekitten.com/39/38" /></label>
CSS
input {
display: none;
}
input[type="radio"]:checked + label {
background-color: green;
}
img {
opacity:0.4;
filter:alpha(opacity=40); /* For IE8 and earlier */
}
I'm not sure you can set css like that for a radio button.
BUT! you can set:
-webkit-appearance: none;
appearance: none;
vertical-align: bottom;
background: #fff;
border: 1px solid #dcdcdc;
-webkit-border-radius: 1px;
-moz-border-radius: 1px;
border-radius: 1px;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
Maybe change the background color?
But that's about as much as you can change, sorry.
In case that you are already using Bootstrap (both CSS and JavaScript) you could use class selector active to achieve effect like the one you want.
See this fiddle: https://jsfiddle.net/7k0dbgsa/1/
.active
{
color: green;
}
The fiddle has been forked from https://jsfiddle.net/KyleMit/0nevkwyn/