Singular1ty
Singular1ty

Reputation: 2615

jQuery - select all <span> elements and remove their text

Hello guys, I have spent about twenty minutes searching in vain for my answer, and need your help. There are thousands of requests for help on how to select elements with jQuery - but everyone wants to do with with some kind of condition, ie, only select an anchor with a certain ID at a certain Y position on the page.

I have a simple request. How do I select all <span> elements on my page and remove their text?

See the thing is, I have a form, and I have <spans>. When I click the Clear Button input, all fields revert back to default (of course). But I want the span elements to have their text deleted.

The <html> on it is simple:

<input type="reset" value="Clear form" name="Clear Button" class="clear">

And my jQuery:

/* Clear form - used to revert all Spans back to normal */
$('#Clear Button').click(function(){
        $('span').val('');

});

So, the Reset effect works because that's DOM/HTML. But my jQuery is sadly broken.

Can anyone tell me what's going wrong? My script is after the Button declaration, if that helps.

Upvotes: 1

Views: 22832

Answers (2)

ChaseTonyReid
ChaseTonyReid

Reputation: 35

Did you source the jquery.js file into your code, and make sure when your refering to the span instead of leaving it "" (blank) use the function $('span').hide Hope i helped

Upvotes: 1

Joseph Silber
Joseph Silber

Reputation: 219938

Your problem is with your button selector. You are not selecting your button. Use this instead:

/* Clear form - used to revert all Spans back to normal */
$('input[name="Clear Button"]').click(function(){
        $('span').text('');
});

Or use $('input[type="reset"]') if that's the only one there...

Upvotes: 13

Related Questions