2

It feels like jQuery is messing with me, although it's probably a simple issue. My code get's to the DOM-element that i want to get the value of, but when i add .val() it returns an empty string.

$('tr.invoice td[data-attr=Payed] div.true').closest('tr').find('td[data-attr=Expiration]')
Returns this:
[<td data-attr=​"Expiration">​2012-04-22​</td>​,<td data-attr=​"Expiration">​2012-05-21​</td>​,<td data-attr=​"Expiration">​2012-06-18​</td>​]

As you can see the DOM-elements has values, but if i add .val() to the JS-string it returns empty strings only (not undefined, empty)

3 Answers 3

12

Method val() is used for input and select elements.

You should use html() or text() methods instead.

$('tr.invoice td[data-attr=Payed] div.true')
    .closest('tr')
    .find('td[data-attr=Expiration]')
    .each(function() {
        // getting inner HTML
        var html = $(this).html();
    }
);
Sign up to request clarification or add additional context in comments.

Comments

4

.val() is for form elements, not textual values. Use .text() intstead.

Fiddle: http://jsfiddle.net/L8V4A/

EDIT: Apologies on the bad link.

2 Comments

not the down-voter but I changed your link description since you said it was a fiddle.
@BZink: My CMD-C must have missed, apologies, and it's fixed!
3

<td>s don't have values, therefore won't return anything. .val() is only used on form elements like <inputs> and <select> tags. To get the contents of an element, use .html() or .text() instead.

The difference between .html() and .text() is that the former will include the tags in the returned string, while .text() will only return the contents of any tags. With this example HTML:

<div>
    <strong>
        Hello world
        <span>How're you doing?</span>
    </strong>
</div>

$('div').html() gives this string (formatted nicely for clarity):

<strong>
    Hello world
    <span>How're you doing?</span>
</strong>

Whereas $('div').text() would give this:

Hello worldHow're you doing?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.