Question

I am beginner with html and javascript, i have a example that i need to hide some text and that text will not take any place on my web. So, I set it's CSS to display:none. But i want get some text inside it. Can any one tell me how to get it? or some ways to do it. This is my example:

<td class="HiddentText" style="display:none;">My Hidden Text</td>

i was tried:

$('.HiddentText').val() 
$('.HiddentText').text()

but i got nothing from it.

Was it helpful?

Solution

I have created a JSFiddle for this problem and I only got it working when I added the required <table> and <tr> statements around the <td> element:

<table>
    <tr>
        <td class="HiddentText" style="display: none;">My Hidden Text</td>
    </tr>
</table>

Then you can do:

$(document).ready(function() {    
    alert($('.HiddentText').html());
});

See my working JSFiddle: http://jsfiddle.net/kgjux/

And my non-working JSFiddle (which does not have the <table> and <tr> elements): http://jsfiddle.net/44Unf/

OTHER TIPS

You actually had the right answer: .text() method.

  1. .val() will give you value of an element that actually has a value (like input, textarea)
  2. .html() is to get whatever HTML code is inside the element
  3. .text() will give you text (visible or not visible to user) inside the element, but with no html formatting, just text.
$('.HiddentText').html()); // for class
$('#HiddentText').html()); // for id

value from elements like paragraph, div, td, span etc are taken through .html() in jquery or .innerHTML() in plain javascript

You need add ';' at the end of function. $('.HiddentText').val(); means return value="something"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top