开发者

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

开发者 https://www.devze.com 2023-04-07 12:35 出处:网络
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

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 B开发者_运维技巧utton 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.


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...


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号