guzman
guzman

Reputation: 187

JQuery .val() returning html (not the value of the input field)

Given the html:

<div id="rearGearInputContainer">
    <input id="rear1"></input>
    <input id="rear2"></input>
    <input id="rear3"></input>
    <input id="rear4"></input>
</div>

In the Chrome console I type:

$('#rearGearInputContainer').find("input").each(function () {return $(this).val();})

And the result is an array of html:

[<input id=​"rear1">​, <input id=​"rear2">​, <input id=​"rear3">​, <input id=​"rear4">​]

...not an array of the input field values as I'd expect. Can anyone explain why? Thanks.

Upvotes: 0

Views: 1000

Answers (2)

George
George

Reputation: 36784

You are trying to return in an each method, which I don't think you want. I think you mean to use the map() method:

var vals = $('#rearGearInputContainer').find("input").map(function () {
    return $(this).val();
}).get()

JSFiddle

Upvotes: 5

caramba
caramba

Reputation: 22480

http://jsfiddle.net/375N3/

$('#rearGearInputContainer input').each(function () {
   alert($(this).val());
})

Upvotes: 0

Related Questions