Michael Irigoyen
Michael Irigoyen

Reputation: 22957

jQuery returns 'undefined' for .val() when one absolutely exists

I am trying to obtain the .val() of a input field within an AJAX loaded box. I absolutely know a value exists, however when I try to access the value using jQuery's .val(), it always returns undefined.

(Both these console.logs are fired right after each other.)

console.log(this.content);

Console output for this.content


console.log($('#sa', this.content).val());

or

console.log(this.content.find('#sa').val());

Returns 'undefined'


Just doing a log on the element shows no value either:

console.log($('#sa', this.content));

enter image description here

I'm at a loss on why I cannot access this value.

There's a million of these types of questions, so feel free to mark as duplicate if you know of one that asks exactly what I'm asking.

Upvotes: 2

Views: 333

Answers (1)

James M
James M

Reputation: 16728

jQuery.find looks for descendants, but the element you're looking for isn't a descendant (it's actually part of the collection.)

Try filter instead:

this.content.filter('#sa').val()

Upvotes: 5

Related Questions