Last active
March 8, 2018 11:33
-
-
Save bampakoa/133783a6e3cb0961761d95e1732e8d56 to your computer and use it in GitHub Desktop.
Iterate over Squarespace form fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Y.on('domready', function() { | |
var data = []; | |
var item; | |
var fields = Y.one('form').all('input, textarea, select, checkbox')._nodes; | |
for(var i=0; i<fields.length; i++){ | |
item = {}; | |
if (fields[i].type !== "radio") { | |
if(fields[i].type !== 'hidden') { | |
// fields that are wrapped in a <label> tag | |
if (fields[i].parentNode.nodeName === 'LABEL'){ | |
item.name = fields[i].parentNode.innerText.trim(); | |
} | |
// fields that are after a <label> tag | |
else if (fields[i].parentNode.children[0].nodeName === 'LABEL'){ | |
item.name = fields[i].parentNode.children[0].innerText.trim(); | |
} | |
} else { | |
item.name = fields[i].name; | |
} | |
} | |
else { | |
if(fields[i].checked) { | |
item.name = document.querySelector('div[for=' + fields[i].name + ']').innerText.trim(); | |
} | |
} | |
if(item.name){ | |
item.value = fields[i].value; | |
data.push(item); | |
} | |
} | |
console.log(data); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment