Solved

Passing a JSON object to a javascript function

Posted on 2014-04-02
3
3,176 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!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

773 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