treehouse : what would you like to learn today?
Web Design Web Development iOS Development

jQuery - How to find what the text is inside an element.

  • I was trying to figure out how to get the text off a page in a <TD> with a Class. That way I can do a conditional statement on the return value such as either hide or show another DIV.

    I tried the following with no luck:

    jQuery.noConflict();
    jQuery(document).ready(function(){


    var giftopt = jQuery(\"#giftopt\");

    if (\"#giftopt == 'Someone Else'\") {
    //do something
    };
    if (\"#giftopt == 'Me'\") {
    //do something
    };
    });
  • You use the .text() function to get the text out of an element

    http://docs.jquery.com/Attributes/text

    var testText = $(\"td#giftopt\").text();