Solved

object passed as reference

Posted on 2016-11-09
1
12 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
Comment Utility
Ok, helps if I return the array
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
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…

771 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

10 Experts available now in Live!

Get 1:1 Help Now