Copyright ©

Prototype Form serialize() Method

previous next


This method is used to serialize form data to a string suitable for Ajax requests (default behavior) or, if optional getHash evaluates to true, an object hash where keys are form control names and values are data.

Depending of whether or not the optional parameter getHash evaluates to true, the result is either an object of the form {name: "johnny", color: "blue"} or a string of the form "name=johnny&color=blue", suitable for parameters in an Ajax request.


formElement.serialize([getHash = false]);

Return Value :

Here are two hints on how it works. For a detail look at the example below.

// 'username=sulien&age=22&hobbies=coding&hobbies=hiking'
// {username: 'sulien', age: '22', hobbies: ['coding', 'hiking']}


<title>Prototype examples</title>
   <script type="text/javascript" 

function showResult()
   var form = $('example'); 
   var element = form.serialize(); 
   alert("form.serialize() : " + element.inspect());



  <p>Click the button to see the result.</p>
  <br />

  <form id="example" action="#" onsubmit="return false">
  <fieldset><legend>User info</legend>
  <div><label for="username">Username:</label> 
  <input name="username" id="username" value="Sulien" type="text">
  <div><label for="age">Age:</label> 
  <input name="age" id="age" value="23" size="3" type="text">
  <div><label for="hobbies">Your hobbies are:</label>
  <select name="hobbies" id="hobbies" multiple="multiple">

  <br />
  <input type="button" value="Result" onclick="showResult();"/>


To understand it in better way you can Try it yourself.

previous next

Copyright ©