0

I have the following HTML:

<label>Lobby Template:</label>

I want to convert it to jquery object and add an attribute id with a value of test:

<label id="test">Lobby Template:</label>

6 Answers 6

3
var $el = $('<label>Lobby Template:</label>').attr('id', 'test');
Sign up to request clarification or add additional context in comments.

Comments

2

I like this syntax:

var $el = $("<label>", {
    id: "test",
    text: "Lobby Template:"
});

Comments

0

use jquery attr

$('label').attr('id', 'test');

Comments

0
$('label')[0].id="test";

You need to have some sort of hook to identify your element. http://jsfiddle.net/RQ3vH/1/ is a demo.

Comments

0

You can do it like this

$('label').attr('id','test');

Comments

0
labels = $('label')
$.each(labels, function(i, label){
    if($(label).html() == 'Lobby Template:' ){
    $(label).attr("id", "test")
    }
})

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.