Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

What would be the way best to convert form elements to an JavaScript Element?

I would like some automatic way to build JavaScript objects, without having to loop each element. I do not want any strings returned like in var formData = f.serialize();.

My HTML

<script type="text/javascript">
$(function() {
    $("#sendrfr").click(function() {

        var f = $("#Commtnx");
        var formData = f.serialize();

        alert(formData);
    });
}); 
</script> 

 <div id="outter">
      <form id="Commtnx" action="/Home/Test1" method="post" name="down">
        <div id="inner">
            <input id="input1" type="text" value="2" />
        </div>    
      </form>
  </div>

 <input type="submit" id="sendrfr" />
share|improve this question
2  
Welcome to SO. Please visit the help center to see how and what to ask. Your question as it is now is off topic. You need to show effort and code. You do not show enough input and expected output. Please create a Minimal, Complete, and Verifiable example using the <> snippet editor – mplungjan Oct 12 at 7:42
    
if your form contains only input elements then you can do var data = $('#formID input'); This will give you array of elemets, Similarly you can add select, textarea too into the selector. – Reddy Oct 12 at 7:42
up vote 1 down vote accepted

serializeArray() does just that.

 $('form').on('submit',function(e){
    e.preventDefault();
    var output = $(this).serializeArray();
    console.log(output);
 });
share|improve this answer

Use .serializeArray() to serialize <form> elements to an array of objects.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.