1+ Serialize HTML controls in any HTML container not compulsorily a form element
2+
3+
4+ Sampson Orson Jackson
5+ sampson.orson@colourblendcreative.com
6+
7+ 28.09.2011
8+
9+ serializeControls.js: As we know, to serialize HTML controls, it has to be within
10+ the context of a form element and be initiated by a submit button. Many developers
11+ working with php may not pinched by that constraint, but a .Net developer; where
12+ we can't have more than one form element nested on the same page and with the use
13+ of Master Pages you can't avoid it.
14+
15+ This control allows you to have virtual forms (as divs, span, ...) that can
16+ contain HTML controls. The plugin by default returns a mapped serialize object, like;
17+
18+ [{"name":"Fname","value":""}]
19+
20+ and by setting the option "json" to true you can have to result look like;
21+
22+ {"Fname":""}
23+
24+ You can also specify another property "propertykey" to the name of a custom attribute
25+ to be specified on each HTML control within the container. This will ensure you have
26+ user friendly names like;
27+
28+ "firstname"
29+
30+ instead of
31+
32+ "_ctl45__ctl0__ctl0_cntrlEmplRgstrtnFrm__ctl0_txtFirstName"
33+
34+ as we get in .Net. It can also be useful to non-asp.net developers that user naming
35+ conventions for their controls and don't want their JSON propery name to contain those.
36+
37+ Note: You can also use the json2.js (https://github.com/douglascrockford/JSON-js/blob/master/json2.js)
38+ to convert the return object to a JSON string and to parse it back if you want to. Also see how the
39+ "propertykey" option is used in the input element "Fname".
40+
41+ Example
42+
43+ **** HTML
44+
45+ <div id="myFormContainer" name="myFormContainer">
46+ First Name:<input type="text" name="Fname" maxlength="12" key="firstname" size="12"/> <br/>
47+
48+ Last Name:<input type="text" name="Lname" maxlength="36" size="12"/> <br/>
49+
50+ Gender:<br/>
51+ Male:<input type="radio" name="gender" value="Male"/><br/>
52+ Female:<input type="radio" name="gender" value="Female"/><br/>
53+
54+ Favorite Food:<br/>
55+ Steak:<input type="checkbox" name="food[]" value="Steak"/><br/>
56+ Pizza:<input type="checkbox" name="food[]" value="Pizza"/><br/>
57+ Chicken:<input type="checkbox" name="food[]" value="Chicken"/><br/>
58+
59+ <textarea wrap="physical" cols="20" name="quote" rows="5">Enter your favorite quote!</textarea><br/>
60+
61+ Select a Level of Education:<br/>
62+ <select name="education">
63+ <option value="Jr.High">Jr.High</option>
64+ <option value="HighSchool">HighSchool</option>
65+ <option value="College">College</option>
66+ </select><br/>
67+
68+ Select your favorite time of day:<br/>
69+ <select size="3" name="TofD">
70+ <option value="Morning">Morning</option>
71+ <option value="Day">Day</option>
72+ <option value="Night">Night</option>
73+ </select>
74+
75+ <p><input type="button" class="mybutton" /></p>
76+ </div>
77+
78+
79+ ***** jQuery
80+
81+ $(".mybutton").click(function() {
82+ //$("#myFormContainer").serializeControls();
83+ var rtVal = $("#myFormContainer").serializeControls({
84+ "propertykey": "key",
85+ "json": true
86+ });
87+
88+ console.log(JSON.stringify(rtVal));
89+ });
0 commit comments