tags:

views:

88

answers:

4

Hi All,

I am trying to pass variable though the GetDetail function below, i can pass string/number it works properly

but unable to pass variable

detLink.onclick = new Function ("GetDetails()");
detLink.setAttribute('onclick',"javascript:GetDetails()")

regards hemant

A: 
detLink.setAttribute('onclick',"javascript:GetDetails("+yourVariableName+")")

When You set attribute You are using string datatype, thus you have to stitch function name with variable VALUE

Rafal Ziolkowski
FYI: http://webbugtrack.blogspot.com/2007/08/bug-242-setattribute-doesnt-always-work.html
Crescent Fresh
Not working for IE in instead of localhost i use IP address if i write on Alert in Get detail it doesnot work
Ank
+5  A: 
detLink.onclick = function () { GetDetails ( parameter1, parameter2, ... );  }

which is an anonymous function.

Read also The function expression (function operator)

A function expression is similar to and has the same syntax as a function declaration

function [name]([param] [, param] [..., param]) {
   statements
}

name The function name. Can be omitted, in which case the function becomes known as an anonymous function.

param The name of an argument to be passed to the function. A function can have up to 255 arguments.

statements The statements which comprise the body of the function.

rahul
A: 
var val1 = "Hell world"
detLink.onclick = GetDetails( val1 );


function GetDetails( myVar ){
   alert ( myVar );
}
Rakesh Juyal
this won't work as GetDetails will be called during the declaration and not after the click.
Ghommey
A: 

If you have access to the variable in question when you set the click handler,

detLink.onclick = function() { 
  GetDetails(foo); 
}

If you don't,

detLink.id = uniqueId;
detLink.onclick = function() {
  var foo = globalData[this.id];
  GetDetails(foo);
}

// somewhere else in your code, where you know the value of the variable
globalData[detLink.id] = foo;
Moishe