Solved

Passing a JSON object to a javascript function

Posted on 2014-04-02
3
3,101 Views
Last Modified: 2014-04-03
I am trying to pass a JSON object to a function.  For some reason, it's not working and the javascript errors out.

The Error I get is:
SyntaxError: missing ] after element list
http://localhost/javascript/jquery-1.10.2.min.js
Line 4.

I generate the button using this js code:
menu = "<button id=\"mttrmenubtn_"+rowObj.ticket_num+"\" class=\"small blue\" onmouseover=\"vw_showMTTRMenu("+rowObj+");\"><i class=\"icon-large icon-expand\"></i></button>"; 

Open in new window

     

The rowObj is correct and is a JSON object.

When the actual HTML gets generated, it ends up looking like this:
<button id="mttrmenubtn_04CVG" class="small blue" onmouseover="vw_showMTTRMenu([object Object]);">
<i class="icon-large icon-expand"></i></button>

Open in new window


An of course it errors out on the called function.  
How can I pass a reference to the object through this generated code?

thanks
0
Comment
Question by:mamuscia
3 Comments
 
LVL 29

Expert Comment

by:QPR
Comment Utility
shouldn't there be a comma between object and object within  [ ] ?
0
 
LVL 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
Comment Utility
You can't pass the actual object - you need to pass a reference to it, so when you create your HTML, use something like this:

onmouseover=\"vw_showMTTRMenu(rowObj)\"

Now as long as rowObj exists in your page (probably as a global var), it will get passed into the function
0
 

Author Closing Comment

by:mamuscia
Comment Utility
Thanks...just using the reference works.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
This article discusses four methods for overlaying images in a container on a web page
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
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)

763 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

9 Experts available now in Live!

Get 1:1 Help Now