0

how can i get all input fields values to an array in jquery?

please see my codes bellow:

<input type="text" name="a" value="a" />
<input type="text" name="b" value="hello" />
<input type="text" name="c" value="world" />

<script type="text/javascript">
    function get_fields_values(){

        // too much code
        var arr=[];
        $.each($("input"),function(i,n){
            arr.push(n.value)
        }); 
        return arr;

        // is there any jquery method to get fields values to an array?
        // $('input').xxx() ?
    }
</script>
1
  • I recommend using a <form> element with .serializeArray(). Commented Jun 9, 2014 at 4:23

3 Answers 3

5

Try to use .map() along with .get() to accomplish your task,

var arr = $("input").map(function(){ return this.value; }).get();

Full code would be,

function get_fields_values(){
    return $("input").map(function(){ return this.value; }).get();
}

Demo

Sign up to request clarification or add additional context in comments.

1 Comment

@anonymousUser: Thanks for your effort in making a demo for my post.!
3

You could use .map method.

function get_fields_values() {
  return $('input').map(function() { return this.value; }).get();
}

Comments

0
function get_fields_values() {
  return $('input').map(function() { return this.value; }).get().join("$");
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.