I have the following css and html...
.hide
{
display:none;
}
<div>
<span class='kardashian 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 can simply use the jQuery :visible or :hidden selector to select all the visible or hidden elements in an HTML page. The jQuery :visible selector considered an element visible if they consume space in the document.
The style display property is used to hide and show the content of HTML DOM by accessing the DOM element using JavaScript/jQuery. To hide an element, set the style display property to “none”. document.
jQuery text() Method The text() method sets or returns the text content of the selected elements. When this method is used to return content, it returns the text content of all matched elements (HTML markup will be removed).
jQuery hide() Method The hide() method hides the selected elements. Tip: This is similar to the CSS property display:none. Note: Hidden elements will not be displayed at all (no longer affects the layout of the page). Tip: To show hidden elements, look at the show() method.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With