0

I am using jquery to append content to my html. In the onclick function, I want to send a javascript variable to a function but it is not working.

Here is the code:

var link = "some url";
$('#header').append('<a onclick="activateLink(link);">Click</a>');

function activateLink(vlink)
{
    window.open(vlink, '_blank');
}

I have tried the following

$('#header').append('<a onclick="activeLink**("'+link+'")**;">Click</a>');

AND

$('#header').append('<a onclick="activeLink**('+link+')**;">Click</a>');

nothing seems to work. any help is appreciated.

1
  • Is link a local variable? Inline Javascript can only access global variables. Commented Sep 29, 2013 at 21:51

1 Answer 1

3

I'd suggest:

$('<a />', {
    'text' : 'Click',
    'click' : function(){
        activateLink(link);
     }
}).appendTo('#header');

JS Fiddle demo.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.