I am trying to find a data attribute from an html input in jquery. Using
$(this).attr("data-jsonid"); in the code below:
$.fn.serializeObject = function () {
var o = {};
var a = this.serializeArray();
$.each(a, function ()
{
if(!isNaN(this.name))
{
var currentId = $(this).attr("data-jsonid");
if (currentId !== undefined)
{
alert(currentId);
}
if (o[this.name] !== undefined)
{
if (!o[this.name].push)
{
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
}
else
{
o[this.name] = this.value || '';
}
}
});
return o;
};
$(function () {
$('form').submit(function () {
$('#result').text(JSON.stringify($('form').serializeObject()));
return false;
I can see in my html source that all of my text inputs have a data-jsonid attribute, but I am unable to ever determine what it is in javascript/jquery. I can only find the name and value when debugging.
I’ve created a jsfiddle project to aid in this explanation, http://jsfiddle.net/mvargos/dSz38/. If you click “Submit Query” the json of the page is printed. My end goal is that I want to be able to read the data-jsonid attribute so that I can format the way my json is saved if this attribute exists.
Currently the json is saved as
`{"1028":["Matt","Varg","27","2","Cris","Vargz","23","A"]}`
but once I can determine the data-jsonid, I would like to create the json similar to:
`"1028":[{"id":"1", "tenantInfo":["Matt","Varg","27","2"]},{"id":"2", "tenantInfo":["Cris","Vargz","23","A"]}]`
I am unsure if I am just doing something wrong syntax wise or making some other error. Thank you for your help and please let me know if this explanation is poor.
Before serializing, you can ‘remember’ the jQuery version of
this:And then you can use $this, when you want to use it as a jQuery object.