0

I'd like to retrieve some input box' value of my page to proceed to an ajax request, so I wrote :

    var account_data = ['firstname', 'lastname', 'e-mail'];
    $.each(account_data, function(index,value){
       var tmp = '[name='+value+']';
       data.value = $(tmp).val();
    });

But it doesn't work when I alert the values in my console (it returns undefined)

1

2 Answers 2

1

use input selector with name attributes

try this

data={};  //create new object
var account_data = ['firstname', 'lastname', 'e-mail'];
$.each(account_data, function(index,value){
   var tmp = 'input[name="'+value+'"]'; //here
   //alert($(tmp).val());
   data[value] = $(tmp).val();  //push the value to the data object
});

now you can proceed the data with the ajax request

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

Comments

1

If it's all the inputs you could do:

var account_data = $.map( $('input'), function() { return this.value; });

otherwise:

var account_data = ['firstname', 'lastname', 'e-mail'],
    data = {};

$.each(account_data, function(index,vals){
   data[vals] = $('[name="'+vals+'"]').val(); //you're overwriting data.value
});

console.log(data);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.