Alex
Alex

Reputation: 1606

How do I get the text of an element using jQuery?

I’m comparing a variable with text that needs to be found inside a child element:

var selectedTVshow = 'something';
$('.tvshow').on('click', function() {
  checkIfDuplicate(selectedTVshow);
})

function checkIfDuplicate(show) {
  $('.container__list-of-shows li').each(function() {
    var tvShowTitle = $(this).find('.container__list-of-shows__info__title').text;
    console.log(tvShowTitle);
    if (tvShowTitle === show)
        $('body').append('<p>true</p>');
      // return false
  })
}

See demo: http://jsfiddle.net/4Kh5S/1/

However, it’s not returning the text of that object. What am I doing wrong?

Upvotes: 0

Views: 56

Answers (2)

Vahid Taghizadeh
Vahid Taghizadeh

Reputation: 997

Check this code :

var selectedTVshow = 'something';
$('.tvshow').on('click', function() {
  checkIfDuplicate(selectedTVshow);
})

function checkIfDuplicate(show) {
  $('.container__list-of-shows').each(function() {

    var tvShowTitle = $(this).find('.container__list-of-shows__info__title').text();
   console.log(tvShowTitle)
    if (tvShowTitle === show)
        $('body').append('<p>true</p>');
      // return false
  })
}

Upvotes: 0

Hacknightly
Hacknightly

Reputation: 5164

You need to use .text() instead of .text

Upvotes: 6

Related Questions