Will
Will

Reputation: 1174

Using JQuery to find the contents of a td tag

Im in the process of learning JQuery and trying to get the contents of a particular td tag. Here is my table and here is my code to try to get the contents. If you could point what im doing wrong, it would be very appreciated. When I click the button the alert message give me a blank.

<table>
  <tr>
     <td>&nbsp;</td>
     <td class="hamburger">Hamburger</td>
     <td>&nbsp;</td>
     <td>&nbsp;</td>
  </tr>
</table>

JQuery:

alert($("#hamburger").text);

Upvotes: 0

Views: 1109

Answers (4)

gillyb
gillyb

Reputation: 8910

The '#' character in the selector is to choose an element by the id.
You should write your selector like this : '.hamburger'

also, the 'text' method you're using, needs to be called like this : $('.hamburger').text()

using the '.' (dot) character in the beginning of the selector, means you're looking for an element by it's class name.

Upvotes: 2

Ethan Brown
Ethan Brown

Reputation: 27292

text is a function, so you need to call it as a function:

alert( $('.hamburger').text();

Also, you are trying to reference hamburger as an ID, not a class; see above how I've used a dot instead of a hash to select it.

Upvotes: 0

Shyju
Shyju

Reputation: 218922

hamburger is a class name so use . for selection. text is a method. not a property.

 alert($(".hamburger").text()); 

Sample : http://jsfiddle.net/yAqLU/1/

use dot for selecting with css class names

use # for selecting with element Id

Upvotes: 0

gabitzish
gabitzish

Reputation: 9691

replace # with . in your selector. # is for id

$(".hamburger").text();

Upvotes: 0

Related Questions