Ejaz Karim
Ejaz Karim

Reputation: 3696

How to get TD value using jQuery.?

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.

Upvotes: 4

Views: 66876

Answers (3)

Sruthi Mamidala
Sruthi Mamidala

Reputation: 361

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();
});

Upvotes: 6

Adil
Adil

Reputation: 148110

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();

Upvotes: 6

Arvind Bhardwaj
Arvind Bhardwaj

Reputation: 5291

Just write

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

Or to get the HTML use,

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

Upvotes: 12

Related Questions