tags:

views:

40

answers:

2

With this html:

<label for="DateOfBirth">
    <span>* </span>
    Date Of Birth:
</label>

How do I select and remove the span element using JQuery?

+7  A: 

Like this:

$("label[for='DateOfBirth'] span").remove();

This selector matches all <span> elements inside <label> elements that have a for attribute of DateOfBirth.

You could also write span:has('*') to only match <span> elements that contain a * in their text.

SLaks
A: 

Actually there are many ways I guess. Question is which one is faster -and maybe more readable.

But remember, if you add more spans there

$("label[for='DateOfBirth'] span").remove();

will remove all of them. So if you want to be more specific you can give an id to your span and reach it with the id you passed.

Or you can use an indexer to reach the first span in your label.

see:

http://docs.jquery.com/Selectors/eq#index

burak ozdogan