I am new to JSON and tried the below example to see the results but it returns an empty array in the console. Any suggestions?
function createJSON() {
var obj = [];
var elems = $("input[class=email]");
for (i = 0; i < elems.length; i += 1) {
var id = this.getAttribute('title');
var email = this.value;
tmp = {
'title': id,
'email': email
};
obj.push(tmp);
}
var jsonString = JSON.stringify(obj);
console.log(jsonString);
}
createJSON();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
$("input[class=email]")into$('input[class="email"]'). You need to add quotes for attributes values in a selector. And since it's a class, you can use$('input.email').[class="email"]and[class=email]will both work - although.emailwould be far more performant and better practice.