Solved

object passed as reference

Posted on 2016-11-09
1
28 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

839 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