Mallika Iyer
Mallika Iyer

Reputation: 710

How to set the id of a 'label' HTML element?

If I have the following:

             <label for="deletetxt">Delete This Text</label>

What is the 'for' attribute here? Is that the id?

Assuming that I cannot set a class for the label element to style the label element, how do i set css for this element?

Upvotes: 8

Views: 72026

Answers (8)

With razor in Html I don´t find the best way for assign the id of a label, but you can assign the id in this way:

@Html.Label("© Integrantes Grupo:",new { @id="TitleIntegrants"} )

Upvotes: 1

Ariel
Ariel

Reputation: 4500

The For tells the label which element to belong to (which really means that when the label is clicked the element will get the focus).

As for your second question - you can use jQuery:
- If your html is static use $("label:eq(index)")
- If your html is dynamic and you know the id of the element the label belongs to, you can use $("label[for='thatid']")

Upvotes: 2

Alain BECKER
Alain BECKER

Reputation: 798

Two question, two answers:

  1. What is the 'for' attribute here?

    It's here to tell the ID of the <input> element that label refers to. Some browsers will use it to set focus on that <input> element when user clicks on that <LABEL>

  2. how do i set css for this element?

    A. If you want to CSS all label elements :

      label {
         /* your styles */
      }

B. If you want to label that element, just use IDs or classnames as you usually do.

Upvotes: 4

ktr
ktr

Reputation: 31

The for attribute is the input/textarea/select that the label refers to.

You can still assign an id to the label:

<label id="myLabel" for="deletetxt">Delete This Text</label>

You can also wrap the input/textarea/select with the label in order to associate them without the for attribute.

<label id="myLabel">Delete This Text <input ... /></label>

Upvotes: 3

Tomalak
Tomalak

Reputation: 338178

The for attribute contains the ID of the element that the label is for. I always thought this would be quite intuitive...

<label for="SomeTextField" id="SomeLabel">Some text field</label>
<input type="text" id="SomeTextField">

You style a label like any other element:

label {
  font-weight: bold;
  color: red;
}

I always thought this would be quite intuitive, as well. So - what are you really trying to do, the questions you ask are a sign that you have a different problem, actually.

Upvotes: 13

Luqman
Luqman

Reputation: 3110

you can set an id as well as a class http://www.w3schools.com/tags/tag_label.asp

the for "Specifies which form element a label is bound to" so when a user clicks on the label it focuses on the target input.

Upvotes: 1

racerror
racerror

Reputation: 1629

"for" is the id of the form element that the label should be associated with.

You can add an id to the label to reference it directly.

<label for="fname" id="lbl-fname">First:</label>
<input type="text" id="fname" />

Upvotes: 1

Glycerine
Glycerine

Reputation: 7347

HTML Label Tag is used for forms and submittion. it is not the ID, this 'for' should have the same name as the ID of the object connected to it - for example

<form>
<label for='ford'>Ford Car</label>
<input type="radio" name="fordCar" id="ford" />
</form>

Its a usability object really.

Upvotes: 1

Related Questions