views:

32

answers:

3

I have an input

<input type="text" value="hello" />

and I want to get the value of this and using Jquery, save the value in an array.

How can I do this???

A: 
var formValues = $('#your_form').serialize();
var formValuesAsArray = $('#your_form').serializeArray();

To get the value from your input, you should give it a name attribute; for example <input type="text" name="message" /> and get its value by $('input[name=message]').val()

chelmertz
+4  A: 

you could run this code for submitting:

$('formname').submit(function() {
  alert($(this).serialize());
  return false;
});

This would return a string like:

a=1

You could then save this to an array.

Neurofluxation
did I miss something? why are you in a `form`?
Reigel
var arr = [];$('input:text').each(function(){ arr.push($(this).val());});
ccppjava
... There are more and more people saying things like "Why are you doing that?" - Does it really matter (providing the answer is correct)?
Neurofluxation
+1  A: 

using jQuery,

$(document).ready(function(){
   var arr = [];
   arr.push($('#inputID').val());
})

you should give your input an id

<input type="text" value="hello" id="inputID" />
Reigel