Anup
Anup

Reputation: 405

How to pass Object as a parameter in Javascript

I want to pass Object in function. Here is my code

var hyperLinkObj = new Object();
hyperLinkObj.path="abc";
hyperLinkObj.text="abctext";

'<li><a href="#" onclick="onItemClick(hyperLinkObj)">'+description+'</a></li>'

Here I am creating Object called as "hyperLinkObj" Now I am creating "li" tag and putting hyperlink in li Now When I inspect the I am getting something like

'<li><a href="#" onclick="onItemClick([object Object])">'+description+'</a></li>'

So How can I pass the hyperLinkObject , as in the onItemClick I have do many thing on this Object.

See Actually I wanted to do something like this.

1] There is an Object which has many Category So wanted to loop through it. 2] then on Each category item click wanted to call OnItemClcik with hyperLinkObj 3] Then on click of on Hyperlink , wanted to do operation like fetching Hyperlink.path and show the Breadcrumb. 4] and finally I am using Extjs So Ext.Panel.update("Li text");

May be I didnt mentioned that ExtJs.update.. it is not working as per your suggestion..

Can you explain How can I achive this ?

Upvotes: 1

Views: 1795

Answers (2)

Manishearth
Manishearth

Reputation: 16188

Put hyperLinkObj in global scope, and refer to it in the onclick.

hyperLinkObj = new Object();
hyperLinkObj.path="abc";
hyperLinkObj.text="abctext";
Ext.Panel.update('<li><a href="#" onclick=\'onItemClick({path:"'+hyperLinkObj.path+'",text:"'+hyperLinkObj.text+'"})\'>'+description+'</a></li>')

[object Object] is the toString() of an Object(), and contains no details.

Upvotes: 2

Idan Gozlan
Idan Gozlan

Reputation: 3203

use binding events for complex onclick functionality:

document.getElementById('anchorID').onclick=function(){/* some code */}

it will be easier for you. check out this fiddle: http://jsfiddle.net/anurag/amedr/

Upvotes: -1

Related Questions