Jan vH
Jan vH

Reputation: 116

Add a class to prepended text only

How can I add a class to a prepended text, without adding it to the entire label. So far I have this:

$('form label').prepend('*');
$(this).addClass('star');

But I know 'this' refers to the label. I would like to add a class to every asterisk.

Upvotes: 1

Views: 382

Answers (2)

Chris Pratt
Chris Pratt

Reputation: 239400

Well you can't add a class to the * because it's just a text node. You have to wrap it with an element node like a span. And, if you're going to do that much, you can just specify the class at the same time:

$('form label').prepend('<span class="star">*</span>');

Upvotes: 0

iambriansreed
iambriansreed

Reputation: 22251

You have to have a tag in order to add an attribute such as class:

$('form label').prepend('<span class="star">*</span>');

or

$('<span class="star">*</span>').prependTo('form label');

Upvotes: 5

Related Questions