Link to home
Start Free TrialLog in
Avatar of tonelm54
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:-
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();
    }
  });
}

Open in new window


Im assuming I can call this by:-
convert2Arr($("#btnLogin"))

Open in new window


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>

Open in new window


To show what Im trying to do, Ive got it on a jsfiddle:-
https://jsfiddle.net/o62rxyqc/

Any advice of what Im doing wrong?

Thanks in advance
ASKER CERTIFIED SOLUTION
Avatar of tonelm54
tonelm54

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial