Solved

object passed as reference

Posted on 2016-11-09
1
34 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 

Accepted Solution

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
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)

717 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