ADM
ADM

Reputation: 1610

Is it possible to add id to a label tag?

I have:

<label for="gender" class="error">Choose</label>

I would like to add an id to this line via jQuery/javascript so the html would end up like:

<label for="gender" class="error" id="addthisid">Choose</label>

Is it possible? How can I do that?

Upvotes: 1

Views: 1745

Answers (2)

peduarte
peduarte

Reputation: 1677

I am guessing you are trying to do that with jQuery. So here we go.

$(function(){
    $('label').attr('id', 'addthisid');
});

Here is an example: http://jsfiddle.net/peduarte/RkCLR/

Upvotes: 0

karim79
karim79

Reputation: 342775

Like this:

$("label[for='gender']").attr("id", "addthisid");

or:

$("label[for='gender']")[0].id = "addthisid";

or:

$("label[for='gender']").get(0).id = "addthisid";

Upvotes: 2

Related Questions