tonelm54
asked on
object passed as reference
Ive been trying to troubleshoot a function to return a load of values containing in a data attribute, but when the element is passed to the function the object is empty, I dont understand what Im doing wrong.
My function is:-
Im assuming I can call this by:-
The idea is to list all the inputs in a data attribute in a element and return them all in an array. For example:-
To show what Im trying to do, Ive got it on a jsfiddle:-
https://jsfiddle.net/o62rx yqc/
Any advice of what Im doing wrong?
Thanks in advance
My function is:-
function convert2Arr(objThis) {
debugger;
var objArr = {};
$.each(objThis.data("jsonfields").split(" "), function(key, value) {
if ($(value).attr("type") === "checkbox") {
if ($(value).is(':checked')) {
objArr[value] = true;
} else {
objArr[value] = false;
}
} else if ($(value).attr("type") === "radio") {
if(typeof $(value + ":checked").val() != 'undefined') {
objArr[value] = $(value + ":checked").val();
alert("Defined - " + $(value + ":checked").val());
}
} else {
objArr[value] = $(value).val();
}
});
}
Im assuming I can call this by:-
convert2Arr($("#btnLogin"))
The idea is to list all the inputs in a data attribute in a element and return them all in an array. For example:-
<button id="btnLogin" data-jsonFields="#txtUsername #txtPassword #txtArea #txtDrop #radioGender #cbox1 #cbox2 #cbox3">Login</button>
To show what Im trying to do, Ive got it on a jsfiddle:-
https://jsfiddle.net/o62rx
Any advice of what Im doing wrong?
Thanks in advance
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.