开发者

How can I get the text of a hidden element in Jquery?

开发者 https://www.devze.com 2023-01-26 07:29 出处:网络
I have the following css and html... .hide { display:none; } <div> <span class=\'kar开发者_StackOverflow社区dashian hide\'>Kimmy</span>

I have the following css and html...

.hide
{
 display:none;  
}

<div>
    <span class='kar开发者_StackOverflow社区dashian hide'>Kimmy</span>
</div>

with the following jquery.

$('div').live('click', function(){
   alert($('kardashian', this).val());
});

If I remove the "hide" class I get "Kimmy" like I would expect but when it has the "hide" class I get nothing? How can I get the text of a hidden element in Jquery?


You just need a proper .class selector and .text() (.val() is for inputs), like this:

$('div').live('click', function(){
   alert($('.kardashian', this).text());
});

The visibility of the element doesn't really affect anything, it'll work whether it's hidden or not.


Use .text() instead:

alert($('.kardashian', this).text());

The .val() method is used to get the value property of form inputs.


You forgot the class indicator. Use .kardashian

0

精彩评论

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

关注公众号