This question already has answers here:
Is there a CSS selector by class prefix?
(4 answers)
Closed last month.
For radios having different name attribute like(code is in pug format)
input.radio.(name='value1' type='radio')
input.radio(name='value2' type='radio')
how to select all of these with on selector, something like
.radio[name = 'value1'], .radio[name= 'value2']{code}
but if I do like this the code gets too long for more radios.
Is there any other way to do this ?
You can use the * wildcard to select based on a specific attribute. Here is a simple example of using wildcard with input elments.
It selects all the elements containing the word input in the name attribute.
Read more about wildcard.
input[name*="input"]{
width:20px;
height:20px;
}
<input type="radio" name="input1"><br>
<input type="radio" name="input2"><br>
<input type="radio" name="input3"><br>
<input type="radio" name="input4"><br>
<input type="radio" name="input5"><br>
Related
This question already has answers here:
Select elements where attribute is non-empty
(3 answers)
CSS selectors - how to select 'for' in CSS?
(3 answers)
Closed 4 years ago.
I know that input[type="text'] is used to select <input type="text"/>, what does input[text] mean?
This snippet appear in angular tutorial:
body, input[text], button {
color: #888;
font-weight: Cambria, Georgia;
}
Angular tutorial (At buttom of that page)
input[text] means that inputs with attribute text assigned to whatever will be returned:
<input text="123">
<input text="">
<input text>
but this won't be
<input type="text">
Proof:
console.log(document.querySelectorAll('input[text]'))
<input text="123">
<input text="">
<input text>
<input type="text">
Why it is like that in Angular tutorial? Probably a simple typo. They anyway don't have inputs in the provided example.
I would like to get a typed value from a ReactStrap Input component, via the onChange function.
The aim is to use a text input only but be able to get a specific typed value (String or Number in my example below).
<Input valueAs="String" name="input1" type="text" onChange={this.onChange}></Input>
<Input valueAs="Number" name="input2" type="text" onChange={this.onChange}></Input>
You can see the expected type is defined by the valueAs attribute.
I expect to get an event in this.onChange with:
a value as a String for Input 1
a value as a Number for Input 2
What is the best way to do this with React / Reactstrap?
Input component of reactstrap does not have a property called valueAs. To get value in a format you need, you can do:
<Input name="input1" type="text" onChange={(e) => this.onChange(`${e.target.value}`)}/>
{/* to make it integer you can add unary plus sign like so: */}
{/* this.onChange(+e.target.value) */}
I am trying to style an element only when that element has a value attribute.
The value attribute is variable (it's a date), but it's the only thing that changes between "no value" and "has value" which I need to style differently.
Is it possible to use a wildcard in the CSS selector ascertain whether the value attribute is present? E.g:
HTML
<input class="thing" value="variable-something">...</input>
<input class="thing">...</input>
CSS
.thing[value="*"] {
...
}
OR
.thing[value=*] {
...
}
I've tried this solution but use of the " makes it look for a specific string. Doing .thing[value=*] is invalid and won't compile.
Any advice?
<input type="text" value="">
<input type="text">
input[value]{
background: #ccc;
}
Try it yourself https://jsfiddle.net/55rjf0y8/
You can use below code
input[value]{background: red;}
<input type="text">
<input type="text" value="">
I have the following HTML:
<label class="my-class" for="_something">Text Number 1</label>
<label class="my-class" for="_something">Number 2 Text</label>
Basically I have 2 labels with identical class and also identical for= attributes
I know that I can target the attribute by:
label[for="_something"] {
/* input */
}
But how can I differentiate the 2 of them?
Basically I need to use the CSS to hide the second label.
Can I target the attribute by the "name text"? "Number 2 Text" in this example?
It IS indeed possible with CSS. Just use nth-of-type().
HTML:
<label class="my-class" for="_something">Text Number 1</label>
<label class="my-class" for="_something">Number 2 Text</label>
<label class="my-class" for="_something">Number 3 Text</label>
<label class="my-class" for="_something">Number 4 Text</label>
CSS:
label[for="_something"]:nth-of-type(2) {
display: none;
}
PHPFiddle Link: http://jsfiddle.net/ua91bf0b/
Please note: This is a CSS3 feature, and will not work with some of the old browsers.
If you want to hide the label based on it's text (Number 2 Text) then you need to use jQuery. It's "almost" the same since you use css selectors too in jQuery.
$('label[for$="_something"]:contains("2")')
this one for example takes the labels where the for tag ends with "_something" (so it's good with 123_something and asd_something too) adn the label text contains "2".
You can make a function for this for easier use:
function HideLabel(forattr, texttosearch) {
$('label[for$="'+forattr+'"]:contains("'+texttosearch+'")').css('display','none');
}
And use it like:
HideLabel('_something', '2');
I would like to change style of #postBtn, if #textfield is empty, something like
#postBtn:[#textfield.value.length==0]{
border-color:gray;
background-color:gray;
}
In html:
<input id='textfield'>
<input type="button" Value="Post" onClick="post()" id="postBtn">
How do I achieve this without javascript?
Thanks!
Ok, you can add required to your input field like so:
<input id='textfield' required>
<input type="button" Value="Post" onClick="post()" id="postBtn">
And then, using :invalid and the adjacent sibling selector (+), you can style the button if the field is empty like so:
#textfield:invalid + #postBtn {
background-color: red;
}
Here is a fiddle of it in action: http://jsfiddle.net/w7377/
Note: If the text input field is not actually a required field, then this solution is not the way to go. You may have to use a Javascript solution if that's the case.