4

I am trying to get value of td using jquery like this.

$("#id").find(".class").attr('value'); 
//or
$("#id").find(".class").val();

both returns empty string although it has a value. note* i'm trying get value of dynamically created element. thanks in advance.

2

3 Answers 3

12

Just write

$("#id .class").text();

Or to get the HTML use,

$("#id .class").html();
Sign up to request clarification or add additional context in comments.

Comments

6

The val() function is primarily used to get the values of form elements such as input, select and textarea. You need text() or html() function to get the contents of td.

To get text

textOfTd = $("#id").find(".class").text();

To get Html

textOfTd = $("#id").find(".class").html();

Comments

6

HTML:

<table>
    <tr>
        <td class="tdcls">1</td>
        <td class="tdcls">2</td>
        <td class="tdcls">3</td>
    </tr>
    <tr>
        <td class="tdcls">4</td>
        <td class="tdcls">5</td>
        <td class="tdcls">6</td>
    </tr>
</table>

Jquery code to select particular td value:

$(".tdcls").mouseenter(function(){
    var a = $(this).text();
});

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.