0

Please find the code below. When I'm trying to pass the variable tmp_lnk from the onclick event to the function it throws up an error that tmp_lnk is not defined. How do I pass the variable to the function?

if(fig_name[1]==img_name)
{
var tmp_lnk = "Supplementary Notes for Images/"+"6H-"+img_name/img_name+".pdf"
$("#notes1").append('<div><a onclick="On_Click(tmp_lnk)" id="closebutton">NOTES</a></div>')
break;
}}

function On_Click(tmp_link)
{
    window.open("tmp_lnk");
}

}

user1403848
  • 103
  • 2
  • 4
  • 15

3 Answers3

1
$("#notes1").append('<div><a onclick="On_Click(' + tmp_lnk+ ')" id="closebutton">NOTES</a></div>')

And

function On_Click(tmp_lnk)
{
    window.open(tmp_lnk);
}
Soulbe
  • 444
  • 8
  • 14
0

Alternatively, how about this:

$("#notes1").append('<div><a onclick="On_Click('+tmp_lnk+')" id="closebutton">NOTES</a></div>')

That would make the variable content written on the method call, solving your issue.

Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
0

because you declare the tmp_lnk in if make it global or declare out side of the if

var tmp_lnk
if(fig_name[1]==img_name)
{
tmp_lnk = "Supplementary Notes for Images/"+"6H-"+img_name/img_name+".pdf"
$("#notes1").append('<div><a onclick="On_Click(tmp_lnk)" id="closebutton">NOTES</a></div>')
break;
}}

function On_Click(tmp_link)
{
    window.open("tmp_lnk");
}
}

Good Read

JavaScript Variable Scope

Community
  • 1
  • 1
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143