javascript - jQuery function as a property value of an object -
i have object within object contains property value jquery function:
var fields = { id: {}, timestarted: {}, duration: {}, status: {}, name: { field: $("#companyname"), changeevent: $("#companyname").on("click", function(){ alert("bazinga!"); }) } }; i have function goes through values found within fields , tries register change events:
function registerchangeevents(){ $.each(fields, function(field, v){ $.each(v, function(prop, vv){ // check property loop on: if changeevent if (prop == "changeevent"){ vv(); } }); }); } when run registerchangeevents() however, console returns uncaught typeerror: object not function. can wrap value of changeevent function(){...} , make function run fine. reason doesn't seem right approach, though.
please let me know if have ideas.
thanks.
when have this:
changeevent: $("#companyname").on("click", function(){ alert("bazinga!"); }) the code executed , result of .on() original selected objects...which $("#companyname"). changeevent set jquery object, containing element id "companyname".
i change name be:
name: { field: "#companyname", changeevent: function(){ alert("bazinga!"); } } and change function be:
function registerchangeevents(){ $.each(fields, function(prop, v){ var field, changeevent; $.each(v, function(propp, vv){ if (propp === "field") field = vv; if (propp === "changeevent") changeevent = vv; }); if (field && changeevent) $(field).on("change", changeevent); }); } i'm confused 1 thing - call "changeevent", event in original code "click".