Solved

Passing a JSON object to a javascript function

Posted on 2014-04-02
3
3,225 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
ID: 39973833
shouldn't there be a comma between object and object within  [ ] ?
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39974656
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
ID: 39974815
Thanks...just using the reference works.
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

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

856 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