0

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

But I'm unable to pass variable

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

4 Answers 4

5
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.

Sign up to request clarification or add additional context in comments.

Comments

0
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

2 Comments

Not working for IE in instead of localhost i use IP address if i write on Alert in Get detail it doesnot work
0

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;

Comments

-2
var val1 = "Hell world"
detLink.onclick = GetDetails( val1 );

function GetDetails( myVar ){
   alert ( myVar );
}

1 Comment

this won't work as GetDetails will be called during the declaration and not after the click.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.