Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

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?

like image 769
etoisarobot Avatar asked Nov 18 '10 19:11

etoisarobot


People also ask

Can jQuery find hidden elements?

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.

How do I show hidden text in HTML?

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.

What is text () in jQuery?

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

What does jQuery hide () do?

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.


2 Answers

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.

like image 62
Nick Craver Avatar answered Oct 07 '22 13:10

Nick Craver


Use .text() instead:

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

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

like image 31
user113716 Avatar answered Oct 07 '22 13:10

user113716