Solved

object passed as reference

Posted on 2016-11-09
1
19 Views
Last Modified: 2016-11-09
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
0
Comment
Question by:tonelm54
1 Comment
 

Accepted Solution

by:
tonelm54 earned 0 total points
ID: 41881235
Ok, helps if I return the array
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now