Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select Label by Input type

Is there a way to exclude or select Label by input types, something similar to input field?

label:not([type=checkbox])
label[type=checkbox] 
like image 493
InTry Avatar asked Oct 23 '12 13:10

InTry


People also ask

How do you connect input to labels?

To associate the <label> with an <input> element, you need to give the <input> an id attribute. The <label> then needs a for attribute whose value is the same as the input's id .

How do I access a label in CSS?

var element = document. querySelector("label[for=email]"); ...or in JavaScript using jQuery: var element = $("label[for=email]");

How can I get element labels?

Use the textContent property to get the text of a label element, e.g. const text = label. textContent . The textContent property will return the text content of the label and its descendants. If the element is empty, an empty string is returned.

How do you put an input element on the same line as its label?

Using float and overflow attributes: Make a label and style it with float attribute. Now set the label float(position) left or right according to your requirement. This will align your label accordingly. Overflow property for input is used here to clip the overflow part and show the rest.


1 Answers

If you give your checkboxes specific id's that all start with the same thing, you could do the following:

HTML

<input type="checkbox" id="chkTerms" />
<label for="chkTerms">Read terms & conditions</label>

CSS

label[for*="chk"], label[htmlfor*="chk"]
{
css here
}

Probably modern browsers only though. EDIT: I have just tried a fiddle: link and it works in chrome and IE 8 & 9 but not 7. I did not try it in FF.

EDIT2: Just to give an explanation of what is going on here, the square brackets looks for an attribute called for. The the asterix changes the behaviour of the equals from just a plain equals, to mean contains - wherever the for attribute contains "chk" it will apply that style. You can replace the * with a ^ and it will change it to mean starts with instead of contains.

EDIT3: BoltClock has provided a fix for IE7 in a comment, I have updated the answer to include it.

like image 121
Andrew Avatar answered Nov 15 '22 18:11

Andrew