I'm trying to pass a html form structure to json and of course the idea is that the html form is dynamic since every time it can be different and I need a code that passes to me json the elements of the form and its characteristics of this For example:
-From a label h2, p, label take the value:
ej-> > "label": "House", "h2": "house" ..
-From an input text that takes out type, placeholder, maxlength, value.
- Give an input number the same as the input text but adding the min, max .......
-From a checbox that takes me out of this boostrap structure:
<div class="checkbox">
<label><input type="checkbox" value="">Option 1</label>
</div>
<div class="checkbox">
<label><input type="checkbox" value="">Option 2</label>
</div>
<div class="checkbox disabled">
<label><input type="checkbox" value="" disabled>Option 3</label>
</div>
then the value of the label and the input type ...
I have tried to do this for example from this test form:
<form id="conv" name="formulario" action="#" method="#">
<label for="usr">dfdf</label>
<input type="text" size="20" maxlength="44" placeholder="de">
<p>scsc</p>
<p>dd</p>
<div class="checkbox">
<label><input type="checkbox" name="checko1" value="">dcd</label>
</div>
<div class="checkbox">
<label><input type="checkbox" name="checko2" value="">cdcd</label>
</div>
</form>
the script:
$(".convertir").click(function () {
function serializeSchema(form) {
return [].map.call(form.elements, function (el) {
return {
type: el.type,
name: el.name,
value: el.value,
placeholder: el.placeholder,
size: el.size,
maxlength: el.maxlength,
min: el.min,
max: el.max,
step: el.step
};
});
}
;
var form = document.querySelector('#conv');
schema = serializeSchema(form);
console.log(JSON.stringify(schema, null, 4));
but he pulls this out of me in a way I do not want
[
{
"type": "text",
"name": "input_text1",
"value": "",
"placeholder": "de",
"size": 20,
"min": "",
"max": "",
"step": ""
},
{
"type": "checkbox",
"name": "checko1",
"value": "",
"placeholder": "",
"size": 20,
"min": "",
"max": "",
"step": ""
},
{
"type": "checkbox",
"name": "checko2",
"value": "",
"placeholder": "",
"size": 20,
"min": "",
"max": "",
"step": ""
},
{
"type": "submit",
"name": "",
"value": ""
}
]
for example the label, h2 does not take it out ....