I want to create a JSON object entirely from the attributes of HTML elements that share the same class so that the key is say the data attribute and value is the id.
I've tried the code below :
$("#searchBtn").click(function() {
var map = [];
$(".searchTextField").each(function() {
var fieldName = $(this).attr('data-field-name')
map.push({
$(this).attr('data-field-name'): $(this).val()
})
alert(map);
});
})
what I want to achieve is to have some like
{
"id" : 1,
"page" : 5
}
but id and page must be dynamic i.e the text id is taken from an tribute of an HTML element