How to style the parent label of a checked radio input - html

I need to stylize some radio inputs. I tried some solutions from here but none worked for me. Can someone please take a look at this code and tell me what can I do?
This is the HTML:
<div class="controls">
<table>
<tbody>
<tr>
<td>
<label class="radio">
<input type="radio" name="ad_caroserie" value="0">Berlina
</label>
</td>
<td>
<label class="radio">
<input type="radio" name="ad_caroserie" value="1">Break
</label>
</td>
<td>
<label class="radio">
<input type="radio" name="ad_caroserie" value="2">Cabrio
</label>
</td>
</tr>
</tbody>
</table>
</div>
And the CSS:
label.radio {
background: #fcb608;
}
.radio input {
display: none;
}
label.radio input[type="radio"]:checked + label {
background: #000 !important;
border: 1px solid green;
padding: 2px 10px;
}
The CSS doesn't have the desired effect; Can you please help me?
This is some related excerpts of JS:
//If checkboxes or radio buttons, special treatment
else if (jQ('input[name="'+parentname+'"]').is(':radio') || jQ('input[name="'+parentname+'[]"]').is(':checkbox')) {
var find = false;
var allVals = [];
jQ("input:checked").each(function() {
for(var i = 0; i < parentvalues.length; i++) {
if (jQ(this).val() == parentvalues[i] && find == false) {
jQ('#adminForm #f'+child).show();
jQ('#adminForm #row_'+child).show();
find = true;
}
}
});
if (find == false) {
jQ('#adminForm #f'+child).hide();
jQ('#adminForm #row_'+child).hide();
//cleanup child field
if (jQ('#adminForm #f'+child).is(':checkbox') || jQ('#adminForm #f'+child).is(':radio')) {
jQ('#adminForm #f'+child).attr('checked', false);
}
else {
if (cleanValue == true) {
jQ('#adminForm #f'+child).val('');
}
}
}
}
else {
var find = false;
for(var i = 0; i < parentvalues.length; i++) {
if (jQ('#adminForm #f'+parentname).val() == parentvalues[i] && find == false) {
jQ('#adminForm #f'+child).show();
jQ('#adminForm #row_'+child).show();
find = true;
}
}
if(find == false) {
jQ('#adminForm #f'+child).hide();
jQ('#adminForm #row_'+child).hide();
//cleanup child field
if (jQ('#adminForm #f'+child).is(':checkbox') || jQ('#adminForm #f'+child).is(':radio')) {
jQ('#adminForm #f'+child).attr('checked', false);
}
else {
if (cleanValue == true) {
jQ('#adminForm #f'+child).val('');
}
}
}
}
}
function dependency(child,parentname,parentvalue) {
var parentvalues = parentvalue.split(",");
//if checkboxes
jQ('input[name="'+parentname+'[]"]').change(function() {
checkdependency(child,parentname,parentvalues,true);
//if checkboxes
jQ('input[name="'+child+'[]"]').change();
jQ('input[name="'+child+'"]').change();
jQ('#'+child).change();
});
//if buttons radio
jQ('input[name="'+parentname+'"]').change(function() {
checkdependency(child,parentname,parentvalues,true);
//if checkboxes
jQ('input[name="'+child+'[]"]').change();
jQ('input[name="'+child+'"]').change();
jQ('#'+child).change();
});
jQ('#f'+parentname).click(function() {
checkdependency(child,parentname,parentvalues,true);
//if checkboxes
jQ('input[name="'+child+'[]"]').change();
jQ('input[name="'+child+'"]').change();
jQ('#f'+child).change();
});
checkdependency(child,parentname,parentvalues,false);
}

A possibility
At my time of posting, I am not exactly sure what the desired layout should be, but there is one specific problem in the attempted CSS that needs to be addressed.
The adjacent siblings selector:
... separates two selectors and matches the second element only if it immediately follows the first element.
If the <input> is a child of the <label>, it isn't adjacent, so while:
label.radio input[type="radio"]:checked + label
is looking for a label immediately following a :checked input inside a label with the class .radio, nothing like that exists.
To alter the styling of the label in this case, would require a selector that affected the parent, which currently isn't possible.
So, to select the label of the :checked input, we need the label to be adjacent, not the parent.
We can use the for="id" attribute:
A <label> can be associated with a control either by placing the control element inside the <label> element, or by using the for attribute.
As I said, I'm not exactly sure what the desired layout should be, but here's an example using the for attribute, that doesn't look too bad.
div {
display: inline-block;
position: relative;
}
label {
background: #fcb608;
padding: 2px 10px 2px 1.5em;
border: 1px solid transparent; /* keeps layout from jumping */
}
input {
position: absolute;
}
input[type="radio"]:checked + label {
background: #000;
border-color: green;
color: white;
}
<div>
<input id="id1" type="radio" name="ad_caroserie" value="0">
<label for="id1" class="radio">Berlina</label>
</div>
<div>
<input id="id2" type="radio" name="ad_caroserie" value="1">
<label for="id2" class="radio">Break</label>
</div>
<div>
<input id="id3" type="radio" name="ad_caroserie" value="2">
<label for="id3" class="radio">Cabrio</label>
</div>
With <input> as a child of <label>
Using a small JavaScript handler listening for changes to the <form>.
If a change is detected, the triggered function checks if an <input type="radio"> was changed, and if so, if it has a <label> as its parentElement.
If that's true, it checks to see if there's an identically named <input type="radio"> that's a child of a <label> element with the class .checked.
If there is, it removes the class from the <label> before applying the same class to the <label> parent of the <input> target that triggered the whole thing.
let form = document.querySelector( "form" );
form.addEventListener( "change", ( evt ) => {
let trg = evt.target,
trg_par = trg.parentElement;
if ( trg.type === "radio" && trg_par &&
trg_par.tagName.toLowerCase() === "label" ) {
let prior = form.querySelector( 'label.checked input[name="' +
trg.name + '"]' );
if ( prior ) {
prior.parentElement.classList.remove( "checked" );
}
trg_par.classList.add( "checked" );
}
}, false );
label {
background: #fcb608;
padding: 2px 10px 2px 0;
border: 1px solid transparent; /* keeps layout from jumping */
}
label.checked {
background: #000;
border-color: green;
color: white;
}
<form>
<label class="radio"><input type="radio" name="ad_caroserie" value="0">Berlina</label>
<label class="radio"><input type="radio" name="ad_caroserie" value="1">Break</label>
<label class="radio"><input type="radio" name="ad_caroserie" value="2">Cabrio</label>
</form>
Without JavaScript things get difficult (per my original explanation of why it's best to use the for attribute in this case).
We can use the appearance property (with prefixes and reasonable support) to effectively hide the user-agent radio GUI, then use the remaining faceless element to build a fake background for the <label>.
This is very hacky and a great deal less dynamic than the default, since some absolute positioning and specific dimensions are required to pull it off.
It kind of works (in most browsers), but is tricky to enforce sitewide.
Something to play around with though :-)
input {
position: absolute;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
width: 5em;
height: 1.5em;
z-index: -1;
background: #fcb608;
border: 1px solid transparent;
margin: -.1em -.8em;
outline: 0;
}
label {
display: inline-block;
width: 5em;
color: white;
text-shadow: 1px 1px 0px black;
}
input[type="radio"]:checked {
background: #000;
border-color: green;
}
<label class="radio"><input type="radio" name="ad_caroserie" value="0">Berlina</label>
<label class="radio"><input type="radio" name="ad_caroserie" value="1">Break</label>
<label class="radio"><input type="radio" name="ad_caroserie" value="2">Cabrio</label>

Just use jQuery with a new css class "selected" something like this:
on start:
$("input[name='ad_caroserie']:checked").parent().addClass("selected");
and onchange:
$('input[type=radio][name=ad_caroserie]').change(function() {
$("input[name='ad_caroserie']").parent().removeClass("selected");
$("input[name='ad_caroserie']:checked").parent().addClass("selected");
// console.log($("input[name='ad_caroserie']:checked").val());
});

After trying so many time with pure HTML and CSS. I am settling with this simple solution with JavaScript. I think this will help.
function check(btn) {
let label = btn.children;
label[0].checked = true;
}
.lib-radio {
color: #1e1e1e;
font-size: 1.0em;
border-radius: 31px;
font-weight: 400;
width: 450px;
height: 40px;
border: 2px solid black;
padding: 0.5em;
font-family: Lato;
margin: 10px 0 0 0;
}
.lib-radio:hover {
background-color: #000;
color: #FFF;
}
<div class="lib-one-input">
<p class="lib-form-label">Select your gender</p>
<div class="lib-radio" onclick="check(this)">
<input id="s1yes" type="radio" name="mcq1" value="male">
<label for="s1yes"> Yes, our researchers authored it</label><br />
</div>
<div class="lib-radio" onclick="check(this)">
<input id="s1no" type="radio" name="mcq1" value="female">
<label for="s1no"> No, we didn&#39t author it </label><br />
</div>
</div>

<form id="button-form">
<fieldset id="button-set">
<label for="button-1">
<input type="radio" id="button-1" name="button-group"/>
</label>
<label for="button-2">
<input type="radio" id="button-2" name="button-group"/>
</label>
<label for="button-3">
<input type="radio" id="button-3" name="button-group"/>
</label>
</fieldset>
</form>
<style>
#button-set label > * {
opacity: 0; <!-- hideing the radio buttons -->
}
</style>
<script>
function setColor(color) {
document.getElementsByName("button-group").forEach(node => {
node.checked === true
? (node.parentElement.style.background = color)
: (node.parentElement.style.background = "rgba(0, 0, 0, 0.5)");
});
}
window.onload = () => {
document.getElementById("button-form").onchange = evt => {
switch (evt.target) {
case document.getElementsById("button-1"):
setColor("rgba(0, 150, 0, 0.5)");
break;
case document.getElementsById("button-2"):
setColor("rgba(250, 200, 0, 0.5)");
break;
case document.getElementsById("button-3"):
setColor("rgba(250, 0, 0, 0.5)");
break;
}
};
};
</script>

Related

i want to validate my code BUT messages does'nt show under username only shows under password i don't where i go wrong

I write a login form but I can't make it correctly in validation with jQuery.
$(document).ready(function() {
$("#form1").validate({
rules: {
username: {
required: true,
minlength: 6
},
password: {
required: true,
minlength: 5
}
},
messages: {
username: {
required: "name is mandatory"
}
}
});
});
.error {
color: red;
}
p {
font-size: 13px;
font-style: arial;
font-align: left;
}
body {
font-family: calibri, arial, sans-serif;
background-color: powderblue;
margin: 0;
padding: 0;
border: 0;
outline: 0;
}
span.password {
float: right;
padding-top: 50px;
}
.login-form {
margin-top: 5%;
margin-bottom: 5%;
position: relative;
width: 390px;
left: 35%;
height: 500px;
border: 6px solid#ff0000;
padding: 10px;
background-color: #00ffff;
}
.login-form h1 {
font-size: 50px;
text-align: center;
text-transform: uppercase;
margin: 40px;
}
.login-form label {
font-size: 29px;
text-align: right margin:45px;
}
.login-form input[type=text],
.login-form input[type=password] {
font-size: 20px;
width: 350px;
padding: 10px;
border: 0;
outline: none;
border-radius: 5px;
}
.login-form button {
font-size: 16px;
color: white;
background-color: green;
font-weight: bold;
padding: 79px;
width: 60%;
margin: 10px 15px;
padding: 8px 6px;
border: 5px;
cursor: pointer;
}
.login-form button:hover {
border: solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="jquery-validation/dist/jquery.validate.min.js">
</script>
<script src="js/bootstrap.min.js"></script>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.0/jquery.validate.js"></script>
<div class="login-form">
<h1>LOGIN PAGE</h1>
<form action="#" name="form1" id="form1">
<label for="firstname">First Name</label>
<input type="text" name="firstname" id="firstname" placeholder="username"><br>
<br>
<label for="password">Password</label>
<input type="password" name="password" id="password" placeholder="pswd"><br>
<input name="submit" type="submit" id="submit" class="submit" value="Submit">
<span>
<input type="checkbox" id= "remember" name="remember " value="remember me">
<label for ="checkbox" name="checkbox" >Remember me</label>
</span>
<span class="password">Forget <a href="#" >Password ?</a></span>
</form>
</div>
For your use case, HTML5 validation attributes could be used to validate
In the HTML snippet below required, minlength, pattern and max validation attributes were used.
<form action="#" id="formOne" novalidate>
<div class="FormGroup">
<label for="name">Name</label>
<input
type="text"
class="FormGroup__Input"
name="username"
placeholder="username"
data-v-input
minlength="6"
required
/>
<span class="FormGroup__ErrorLabel"></span>
</div>
<div class="FormGroup">
<label for="password">Password</label>
<input
type="number"
name="password"
class="FormGroup__Input"
placeholder="password"
data-v-input
minlength="5"
required
/>
<span class="FormGroup__ErrorLabel"></span>
</div>
<button type="submit">Submit</button>
</form>
Steps to make this form work
put novalidate attribute on the form element to validate inputs using code (javascript).
Each input element should have a name attribute that should be unique to that input
Add data-v-input attribute to each input element you want to be considered for validation by the javascript.
Put your html5 validation rules on the input elements
To show errors, add an element just below the input element. NOTE: The error label should be the next sibling of the input field.
'use strict';
const formNode = document.querySelector("#formOne");
initValidation(formNode, (form, data) => {
alert(JSON.stringify(data, null, 4))
});
function initValidation(formNode, onSubmitHandler){
const validationErrors = [];
const inputs = formNode.querySelectorAll("[data-v-input]");
const selects = formNode.querySelectorAll("[data-v-select]");
formNode.addEventListener("submit", handleSubmit);
inputs.forEach(input => input.addEventListener("blur", handleBlur));
inputs.forEach(input => input.addEventListener("input", handleInput));
function handleSubmit(evt){
evt.preventDefault();
let formData = {};
if(inputs.length > 0){
inputs.forEach(input => {
validateInput(input);
formData[input.name] = input.value;
});
}
if(selects.length > 0){
selects.forEach(select => {
validateInput(select);
formData[select.name] = select.value;
});
}
if(validationErrors.length === 0){
formNode.reset();
onSubmitHandler(formNode, formData);
}
}
function handleBlur(evt){
validateInput(evt.target);
}
function handleInput(evt){
validateInput(evt.target);
}
function validateInput(inputNode){
if(inputNode.validity.valid){
let inputNodeIndex = validationErrors.indexOf(inputNode.name);
validationErrors.splice(inputNode, 1);
renderErrorLabel(inputNode, false);
} else {
validationErrors.push(inputNode.name);
renderErrorLabel(inputNode);
}
}
function renderErrorLabel(node, show = true){
node.nextElementSibling.textContent = show ? node.validationMessage : "";
}
return true;
}
Copy the initValidation function into your javascript file.
To validate your form, evoke initValidation after DOM loads. It takes two arguments.
a form node: a result of document.getElementById or any of the DOM selectors. However, initValidation takes one form node.
a callback that receives the form node as it's first argument and the form's data as the second argument. Whatever you want to do after validation is complete goes in the body of that callback. NOTE: The callback is only called when all validations were passed and there's no error.

Is there a way to clear an input type="number" like when using type="search"

I have a Bootstrap 4 form with various inputs on some number, some text and others email.
I have already sorted my text inputs by adding the below, which is displaying the 'x' in the input
input[type="search"]::-webkit-search-cancel-button {
-webkit-appearance: searchfield-cancel-button;
}
But I want the same for my 'number' and 'email' inputs
I tried using the below to see if it works, but it doesn't
input[type="search"]::-webkit-search-cancel-button {
-webkit-appearance: searchfield-cancel-button;
}
I have also used the below CSS to remove the arrows when using type='number' and again it works fine
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
Thought I'd ask before I revert to using regex for my number inputs
HTML
<input id="one" class="form-control" type="search" value="Test company name" />
<input id="two" class="form-control" type="number" value="123456" />
<input id="two" class="form-control" type="number" value="12345634" />
As you can see the arrows are not displaying for my number inputs which is what I want.
There is good and bad news. The bad news first: the webkit cancel button is only available to input fields of type search.
The good news: you can create the button yourself.
Copy and paste the following CSS:
.close-button {
color: #1e52e3;
font-size: 12pt;
font-family: monospace,sans-serif;
font-weight: 600;
position: absolute;
z-index: 2;
display: none;
}
.close-button:hover {
cursor: pointer;
}
and change the font-size to what is suitable by trial and error.
Then add this Javascript before the </body> tag in the relevant HTML:
<script>
var fields = document.querySelectorAll('input[type=number],input[type=email]');
fields.forEach(function(input) {
let x = document.createElement('span');
x.classList.add('close-button');
x.innerHTML = 'x';
x.style.left = input.clientWidth - 15;
x.onmousedown = removeContent;
input.parentNode.insertBefore(x, input);
input.oninput = toggleCloseButton;
input.addEventListener( 'focusin', toggleCloseButton);
input.addEventListener( 'focusout', hideCloseButton);
});
function toggleCloseButton() {
if (this.value.length > 0) {
this.previousSibling.style.display = 'block';
} else {
this.previousSibling.style.display = 'none';
}
}
function hideCloseButton() {
this.previousSibling.style.display = 'none';
}
function removeContent(){
this.nextSibling.value = '';
this.nextSibling.focus();
}
</script>

Alignment is getting changed or overlapping when I restore down the web browser

My html page div element is getting changed when I restore down the browser but works fine when I maximize it.
when I click on the password field there is a pop-up window displayed to validate the password complexity and every time it will be displayed next to the password field in full screen mode but it will overlap when I click on password field in restore down mode.
I want that to be showed next to the password field in restore down mode also as how it works in full screen mode.
please help
Please find below the HTML and CSS code attached.
var check = function() {
if (document.getElementById('psw').value ==
document.getElementById('confirmPassword').value) {
document.getElementById('info').style.color = 'green';
document.getElementById('info').innerHTML = 'Matching';
} else {
document.getElementById('info').style.color = 'red';
document.getElementById('info').innerHTML = 'Not Matching';
}
}
function myFunction() {
var x = document.getElementById("psw"), y = document.getElementById("confirmPassword");
if (x.type === "password") {
x.type = "text";
y.type = "text";
} else {
x.type = "password";
y.type = "password";
}
}
var psw = document.getElementById("psw");
var letter = document.getElementById("letter");
var capital = document.getElementById("capital");
var number = document.getElementById("number");
var length = document.getElementById("length");
var symbol = document.getElementById("symbol");
// When the user clicks on the password field, show the message box
psw.onfocus = function() {
document.getElementById("message").style.display = "block";
}
// When the user clicks outside of the password field, hide the message box
psw.onblur = function() {
document.getElementById("message").style.display = "none";
}
// When the user starts to type something inside the password field
psw.onkeyup = function() {
// Validate lowercase letters
var lowerCaseLetters = /[a-z]/g;
if(psw.value.match(lowerCaseLetters)) {
letter.classList.remove("invalid");
letter.classList.add("valid");
} else {
letter.classList.remove("valid");
letter.classList.add("invalid");
}
// Validate capital letters
var upperCaseLetters = /[A-Z]/g;
if(psw.value.match(upperCaseLetters)) {
capital.classList.remove("invalid");
capital.classList.add("valid");
} else {
capital.classList.remove("valid");
capital.classList.add("invalid");
}
// Validate numbers
var numbers = /[0-9]/g;
if(psw.value.match(numbers)) {
number.classList.remove("invalid");
number.classList.add("valid");
} else {
number.classList.remove("valid");
number.classList.add("invalid");
}
// Validate length
if(psw.value.length >= 8) {
length.classList.remove("invalid");
length.classList.add("valid");
} else {
length.classList.remove("valid");
length.classList.add("invalid");
}
// Validate Symbols
var symbols = /[-!$%^&*()_+|~=`{}[:;<>?,.##\]]/g;
if(psw.value.match(symbols)) {
symbol.classList.remove("invalid");
symbol.classList.add("valid");
} else {
symbol.classList.remove("valid");
symbol.classList.add("invalid");
}
}
/* Style all input fields */
input {
width: 25%;
padding: 12px;
border: 1px solid #ccc;
border-radius: 8px;
box-sizing: border-box;
margin-top: 6px;
margin-bottom: 6px;
}
#myForm select
{
width: 25%;
padding: 12px;
border: 1px solid #ccc;
border-radius: 8px;
box-sizing: border-box;
margin-top: 6px;
margin-bottom: 16px;
}
/* Style the submit button */
input[type=submit] {
background-color: #4CAF50;
color: white;
}
/* Style the container for inputs */
.container {
background-color: #f1f1f1;
padding: 20px;
}
/* The message box is shown when the user clicks on the password field */
#message {
display:none;
float: left;
background: transparent;
color: #000;
position: absolute;
right: 0;
padding: -2000px;
margin-top: -120px;
margin-right: 200px;
}
#message p {
padding: 1px 35px;
font-size: 14px;
}
/* Add a green text color and a checkmark when the requirements are right */
.valid {
color: green;
}
.valid:before {
position: relative;
left: -35px;
content: "✔";
}
/* Add a red text color and an "x" when the requirements are wrong */
.invalid {
color: red;
}
.invalid:before {
position: relative;
left: -35px;
content: "?";
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body bgcolor="#dbddea">
<h2 align="center"><u>Password Change</u></h2>
<p align="center"><marquee><h3>Change the password for unix users.</h3></marquee></p>
<div class="container">
<form>
<div id=myForm align = "center">
<label for="usrname">Select Username</label><br>
<select name="Users">
<option value="test1">test1</option>
<option value="test2">test2</option>
</select>
</div>
<div align= "center">
<input type="password" id="psw" name="psw" onkeyup='check();' placeholder="New Password" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!##$%^&*_=+-]).{8,}" title="Must contain at least one number, one symbol and one uppercase and lowercase letter, and at least 8 or more characters" required>
<br>
<div id="message" align = "left">
<h4>Password must contain the following:</h4>
<p id="letter" class="invalid">A <b>lowercase</b> letter</p>
<p id="capital" class="invalid">A <b>capital (uppercase)</b> letter</p>
<p id="number" class="invalid">A <b>number</b></p>
<p id="length" class="invalid">Minimum <b>8 characters</b></p>
<p id="symbol" class="invalid">A <b>symbol</b></p>
</div>
<input type="password" id="confirmPassword" name="confirmPassword" onkeyup='check();' placeholder="Re-type Password" title="Confirm new password" required>
<br>
<span id='info'></span>
<input type="checkbox" onclick="myFunction()" style="width: 40px;">Show Password
</div>
<div align = "center">
<input type="submit" id="submit" value="Change Password">
</div>
</form>
</div>
</body>
</html>
Plz add this code..
css
#media only screen and (max-width: 1280px) {
#message {
position: relative;
float: none;
margin: 0;
width: 25%;
}
}
Plz modify your css code..
css
#message {
display:none;
width: 25%;
}
HTML
<div class="container">
<form>
<div id=myForm align = "center">
<label for="usrname">Select Username</label><br>
<select name="Users">
<option value="test1">test1</option>
<option value="test2">test2</option>
</select>
</div>
<div align= "center">
<input type="password" id="psw" name="psw" onkeyup='check();' placeholder="New Password" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!##$%^&*_=+-]).{8,}" title="Must contain at least one number, one symbol and one uppercase and lowercase letter, and at least 8 or more characters" required>
<br>
<input type="password" id="confirmPassword" name="confirmPassword" onkeyup='check();' placeholder="Re-type Password" title="Confirm new password" required>
<br>
<div id="message" align = "left">
<h4>Password must contain the following:</h4>
<p id="letter" class="invalid">A <b>lowercase</b> letter</p>
<p id="capital" class="invalid">A <b>capital (uppercase)</b> letter</p>
<p id="number" class="invalid">A <b>number</b></p>
<p id="length" class="invalid">Minimum <b>8 characters</b></p>
<p id="symbol" class="invalid">A <b>symbol</b></p>
</div>
<span id='info'></span>
<input type="checkbox" onclick="myFunction()" style="width: 40px;">Show Password
</div>
<div align = "center">
<input type="submit" id="submit" value="Change Password">
</div>
</form>
</div>
Here are some changes to make it simple.
#message {
display:none;
float: left;
background: transparent;
color: #000;
position: absolute;
right: 0;
padding: -2000px;
margin-top: -120px;
margin-right: 200px;
}
Replace with below CSS
#message {
width: 25%;
}
Make changes in js code as below
psw.onfocus = function() {
document.getElementById("message").style.display = "block";
}
// When the user clicks outside of the password field, hide the message box
psw.onblur = function() {
document.getElementById("message").style.display = "none";
}
Change to
psw.onfocus = function() {
$('#message').slideDown();
}
// When the user clicks outside of the password field, hide the message box
psw.onblur = function() {
$('#message').slideUp();
}

Color Change after Field Entry - CSS

Is there any css property which can be used to highlight all the filled fields? Just like we have, on focus css properties.
input[type="text"] {
background-color: white;
}
input[type="text"]:focus {
background-color: red;
}
<input type="text">
I want to make the field background-color green when there is something in the field. Is this possible through CSS? If not CSS, is there any other way?
Certainly you can achieve this with javascript.
The script below will listen for a keyup after the focus has shifted to any one of the <input> fields.
It will then check to see if the respective <input> field is empty.
If it is not, it will change the background of the <input> field to green.
var inputs = document.querySelectorAll('input[type="text"]');
function detectContent() {
if (this.value !== '') {
this.style.backgroundColor = 'green';
this.style.color = 'white';
} else {
this.style.backgroundColor = 'white';
this.style.color = 'black';
}
}
for (var i = 0; i < inputs.length; i++) {
var input = inputs[i];
input.addEventListener('keyup', detectContent, false);
}
<input type="text" />
<input type="text" />
<input type="text" />
Try this...
$(document).ready(function() {
$(':input').on('input', function() {
if (this.value.length > 0) {
$(this).css('background-color', 'green');
} else {
$(this).css('background-color', '');
}
});
});
input[type="text"] {
background-color: white;
color: #fff;
}
input[type="text"]:focus {
background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text">
<input type="text">
<input type="text">
<input type="text">

Quad-state checkbox

I can have a tri-state checkbox with indeterminate, but I need to rotate 4 states. So how can I make something to look like a quad state checkbox? Is my only option to create a set of images to look like a checkbox with various icons or is there a more elegant way?
You can achieve similar effect (cycled state something) with radio buttons and a bit of CSS (CSS3), without JavaScript:
.cyclestate {
display: inline-grid;
}
.cyclestate label {
grid-area: 1 / 1;
background-color: white;
color: black;
z-index: 0;
opacity: 0;
}
.cyclestate input {
position: absolute;
z-index: -1;
}
.cyclestate input:checked + label {
opacity: 1;
}
.cyclestate input:first-of-type + label {
z-index: 2;
}
.cyclestate input:checked + label + input + label {
opacity: 0;
z-index: 2;
}
/* -- Unrelated and accessibility -- */
.cyclestate { border: none; padding: 0; }
.cyclestate legend { position: absolute; }
.cyclestate label { padding: 1em; text-align: center; display: inline-block; cursor: pointer; user-select: none;
}
.cyclestate label::before { content: '← '; }
.cyclestate label::after { content: ' →'; }
.cyclestate label::before, label::after { color: transparent; }
.cyclestate input:focus-visible + label { outline: solid; }
.cyclestate input:focus-visible + label::before,
.cyclestate input:focus-visible + label::after { color: currentcolor; }
:root { background: dimgray; color: snow; }
:link { color: aqua; } :visited { color: lime; }
<fieldset class="cyclestate" id="x">
<legend>Pick a number</legend>
<input id="one" type="radio" name="r" checked>
<label for="one">One</label>
<input id="two" type="radio" name="r">
<label for="two">Two</label>
<input id="three" type="radio" name="r">
<label for="three">Two Plus One</label>
<input id="four" type="radio" name="r">
<label for="four">Four</label>
</fieldset>
<p><button onclick="x.classList.toggle('cyclestate')">Toggle condenseness ↑</button>
Basically this approach overlays labels over each other (leveraging quite modern CSS techniques (grid)) and makes label of the NEXT unchecked input transparent and raised above all others. In effect what is visible is checked input's label, but click / tap events are captured by that transparent sibling overlay. This preserves semantics and accessibility.
(Caution: haven't thoroughly tested with screen readers nor older browsers.)
Original 2015 snippet with really dubious accessibility, use with caution (or better do not use at all):
.cyclestate input:not(b),
.cyclestate input:not(b) + label {
display: none;
}
.cyclestate input:checked + label {
display: inline-block;
width: 4em;
text-align: center;
-webkit-user-select: none;
-moz-user-select: none;
-webkit-appearance: button;
-moz-appearance: button;
}
<span class="cyclestate">
<input type="radio" name="foo" value="one" id="one" checked>
<label for="two">one</label>
<input type="radio" name="foo" value="two" id="two">
<label for="three">two</label>
<input type="radio" name="foo" value="three" id="three">
<label for="four">three</label>
<input type="radio" name="foo" value="four" id="four">
<label for="one">four</label>
</span>
With this you'll have space-conserving visual representation of multiple state at one place and readable value on the server side.
Be warned that such usage of label is a kinda exploit: label targets different input than it describes.
A checkbox can be made to look like it can have three states, but it will truly only ever have two.
Also, the "indeterminate" visual state is not reachable from the default user interface and has to be triggered programatically:
var box = document.getElementById('box'),
checked = document.getElementById('checked'),
indet = document.getElementById('indet');
var update = function()
{
checked.textContent = box.checked;
indet.textContent = box.indeterminate;
};
box.addEventListener('click', update);
document.getElementById('a').addEventListener('click', function()
{
box.checked = true;
update();
});
document.getElementById('b').addEventListener('click', function()
{
box.checked = false;
update();
});
document.getElementById('c').addEventListener('click', function()
{
box.indeterminate = true;
update();
});
document.getElementById('d').addEventListener('click', function()
{
box.indeterminate = false;
update();
});
<input type="checkbox" id="box"><br>
<button id="a">On</button> <button id="b">Off</button><br>
<button id="c">Indeterminate</button> <button id="d">Clear indeterminate</button>
<div>box.checked: <span id="checked"></span></div>
<div>box.indeterminate: <span id="indet"></span></div>
So to answer your question:
There is no native way to add a fourth visual (yet even actual) state to a checkbox, but this does not mean that you have to resort to images.
It's easy enough to style an element to look like a checkbox and use some JS to simulate a state rotation:
var state = 0;
var toggle = document.getElementById('toggle');
toggle.addEventListener('click', function()
{
state = (state + 1) % 4;
toggle.className = 'state' + state;
});
#toggle
{
border: solid 1px #666;
border-radius: 3px;
width: 14px;
height: 14px;
}
.state0
{
background: linear-gradient(#DDD, #FFF);
}
.state1
{
background: linear-gradient(#F00, #C00);
}
.state2
{
background: linear-gradient(#FF0, #CC0);
}
.state3
{
background: linear-gradient(#0F0, #0A0);
}
<div id="toggle" class="state0"></div>
But in the end you will have to use something other than actual checkboxes.
Apart from that, having a checkbox-like thing with more that two states is really counter-intuitive, and I kindly ask you to go with something that was made for multiple states, like radio buttons or a dropdown list.
It's not possible today. For others reading, a good reference about the using the three states including indeterminate: https://css-tricks.com/indeterminate-checkboxes/
You'll need to create a different solution using your own icons and in a sense create a custom form control. A possible design that fulfills your need to have multiple states:
Use a list of icons, like the star system in Gmail. When the star is clicked in Gmail it changes color and eventually after enough clicks (up to 10 I think) other icons like exclamation point and checkmark begin displaying.
Associate your different state requirements with the currently used icon (or index of the underlying icon list).