style span when focused on form input - html

Small question but, cannot find method to solve this little problem. I have html form
<div id="todolist">
<span class="add-on">OK</span>
<input class="addtodo" placeholder="New todo task" name="TITLE" type="text" >
</div>
in CSS
#post-todo span{
color:#aaa;
}
I want to change color:#333 when focused on input,
how do this?

use CSS:focus selector
something like below.
input:focus
{
background-color:yellow;
color:blue;
}
For suppose your OK span would be placed after the input then the below code will work without the help you of the jQuery. Only the CSS will do the trick.
<div id="todolist">
<input class="addtodo" placeholder="New todo task" name="TITLE" type="text" >
<span class="add-on">OK</span> <!-- span must be after input-->
</div>
.addtodo:focus + .add-on
{
background-color:yellow;
color:blue;
}
The + in the CSS is used to Matches any element immediately preceded it by a sibling element.
Consider
E + F
{
// code
}
Then Matches any F element immediately preceded by a sibling element E.
Here is the Demo http://jsfiddle.net/UxZXN/

Unfortunately it is not possible with pure css to change the span styling when focussing the input. Using the :focus selector works only for child elements of the focused one.
But there is a fairly simple javascript jquery method :
$("#post-todo .addtodo").focus(function(){
$("#post-todo .add-on").css("color", "#333");
})
$("#post-todo .addtodo").blur(function(){
$("#post-todo .add-on").css("color", "#aaa");
})
See here: http://jsfiddle.net/BEeNa/

It finally become possible, just a few years later...
It is possible with support of :focus-within.
#todolist:focus-within .add-on { color: #aaa; }
<div id="todolist">
<span class="add-on">OK</span>
<input class="addtodo" placeholder="New todo task" name="TITLE" type="text" >
</div>

Related

::placeholder::after doesn't work in CSS

When I apply ::after for my <p> element, it works fine, but when I use it for the ::placeholder pseudo-element on my input fields, it doesn't work:
p::after {
content: "*";
color: red;
}
#registerFirstName::placeholder::after {
content: "*";
color: red;
}
<!DOCTYPE html>
<html>
<body>
<input id="registerFirstName" tabindex="1" name="registerFirstName" title="" alt="" value="" required="required" maxlength="40" aria-required="true" placeholder="First Name" class="error" aria-invalid="true" type="text">
<p>I live in Ducksburg</p>
</body>
</html>
Output:
Can someone help me fixing this?
:after and :before are not supported in Internet Explorer 7 and under, on any elements.
It's also not meant to be used on replaced elements such as form elements (inputs) and image elements.
In other words it's impossible with pure CSS.
However if using jquery you can use
$(".mystyle").after("add your smiley here");
Psuedo elements do not work on empty elements such as <input>.
There are two problems with your code.
First, the pseudo elements can be set only for elements. Not for other pseudo elements.
Second, as others already mentioned, generated content pseudo elements (::before and ::after) are not supposed to work on empty elements (those that have no content between start and end tags in the markup) and usually they don't (there are some exceptions, but, IIRC, the only browser that allowed these pseudo elements for <input> was Opera with Presto engine).
So to add the asterisk in a cross-browser way, you need an extra element. For example, you can do the following:
/* selecting spans immediately following anything with the "placeholder" attribute */
[placeholder] + span::after{
content:"*";
color: red;
}
<input id="registerFirstName" tabindex="1" name="registerFirstName" title="" alt="" value="" required="required" maxlength="40" aria-required="true" placeholder="First Name" class="error" aria-invalid="true" type="text">
<span></span>
UPD: Sorry, I missed the part that the asterisk should be next to the placeholder text at first. Unfortunately, it's impossible with CSS. But you can use the floating label pattern instead of the placeholder, which makes it possible to add the asterisk in the needed place with ::after pseudo element, and also improves the accessibility of the form in comparison to the bare placeholder solution.

How to Apply css to all the text boxes in the form using form id

I am using jquery form validation and I need to apply color code for all the textboxes in the form without specifying each textbox id in the Css. Instead How to apply css for all the text box in the form using the form id. Please let me know is there any solutions.Thanks in advance.
#formID input[type="text"]{
Put your css here
}
in css, you can do this:
form input {
// Your formatting comes here..
}
or if you want this to apply for a specific form having an id="myForm":
#myForm input {
// formatting comes here for example: color:red;
}
The background knowledge for this is CSS Selectors.
If by textboxes you mean <input type="text"> and <textarea> elements, you can simply style them using the following:
#formid input[type=text],
#formid textarea {
/* CSS rules */
}
Note that <input> elements with types other than text exist. Read more about those here.
Try this...
#frm1>input[type="text"]{
border:1px solid red;
width:100px;
margin:1%;
}
<form id="frm1">
<input type="text" />
<input type="text" />
<input type="text" />
<input type="text" />
</form>
You can use the .children() function of the jQuery api to set the css properties of the form elements.
eg : jsFiddle
$('#FormID').children().css("property", "value");

How to style a form element when an input is focused?

I have a form that is structured similarly to the one below. How do I use CSS to style the input[type=submit] element when the input[type=text] is focused?
For example:
<form id="example">
<input type="text" name="search">
<input type="submit" value="Search">
</form>
<style>
#example > input[type="text"]:focus {
// please style input[type="submit"]
}
</style>
I'm trying to accomplish this because a wordpress theme has a search form with the search input and submit button inline. When a user focuses on the input, I want the input field and submit button to have the same border color change.
Alternatively, I could style the entire div around the input and submit button, but then we come back to the problem of styling another element when one element is focused.
Is this even possible with CSS3 or would I have to resort to a javascript function? (Last resort.)
If the structure in your actual code is the same as what you've posted in question, you can use + (adjacent) selector to select the submit button when there's focus on text input.
This is the selector you're looking for: #example > input[type="text"]:focus + input[type="submit"]
To read more about + selector, this answer to "What does the “+” (plus sign) CSS selector mean?" explains it in a short manner and even covers browser support.
Here is a snippet to show it in action. Doesn't look too pretty, but does the job :)
#example > input[type="text"]:focus + input[type="submit"] {
background: gray;
}
<form id="example">
<input type="text" name="search">
<input type="submit" value="Search">
</form>
Here is a way using JQuery, see fiddle https://jsfiddle.net/t33rdra6/2/
$(document).ready(function() {
$('input').blur(function() {
$('input[type="submit"]').removeClass("focus")
})
.focus(function() {
$('input[type="submit"]').addClass("focus");
});
});

How to target an outer Div when checkbox is checked

I want to target a div when a checkbox is checked. Can anybody tell me how can I target an outer div when checkbox is checked?
if($('.checkboxClassName').checked) {
$(this).parent();
}
It will target the parent div that the checkbox is inside, you can use more .parent() if tour target is not inside the same parent.
example: if your code is like this:
<div class="container">
<div class="target"></div>
<div class="parent">
<div class="checkboxDiv">
<input type="checkbox">
</div>
</div>
</div>
and you want to target the div.target you'll need the code like this:
if($('.checkboxClassName').checked) {
$(this).parent().parent().parent().find(".target").css('background','magenta');
} else {
$(this).parent().parent().parent().find(".target").css('background','cyan');
}
those parents will work like this: $(this).parent() = targeting div.checkboxDiv
$(this).parent().parent() = targeting div.parent
...
Note how this jsFiddle highlights the usage in a very simple way:
A check box is focused upon (checked).
The CSS style :checked catches this occurrence and applies a CSS style to the div contents.
The div can be another element you want, just make sure you play around with the code and adapt it to your needs. Let us know if you need any more help!
Source: :checked
HTML
<input type="checkbox" id="ossm" name="ossm">
<label for="ossm">CSS is Awesome</label>
CSS
input[type=checkbox] + label {
color: #ccc;
font-style: italic;
}
input[type=checkbox]:checked + label {
color: #f00;
font-style: normal;
}
Edit: I thought you would like a reference to 'how' CSS works here:
Attribute Selector by value
Element plus Element
...and in general CSS selectors

I am trying to use checkbox to change color of element but it doesn't works

I am trying to use "check box" to change color of element.
HTML:
<label for="toggle-1">I'm a toggle</label>
<input type="checkbox" id="toggle-1">
<div class="reklama">I'm controlled by toggle. No JavaScript!</div>
CSS:
.reklama {
color:Red;
}
input#toggle-1: checked ~ .reklama{
color:green;
}
U can find my demo here.
You can't have any spaces before the ":checked", so that it is directly connected to your id. This will work in your CSS
input#toggle-1:checked ~ .reklama{