user3653444
user3653444

Reputation: 15

How to get Var inside onclick function?

I have 2 variables outside. want to display those 2 variable values inside onclick event.. Cant able to get value using my below code.. please help me to solve this issue. Have to fix this issue in 3 hours.

var myCode = "12345";
var myCount = "5"

$('.cols2.continue').html('<a href="#" onClick="test.element=&#39;myCode, myCount &#39;;test.elemName=+myCode +;">Browse</a>');

Now, i m getting variable name in place of value.

Thanks

Upvotes: 0

Views: 117

Answers (3)

user2864740
user2864740

Reputation: 62005

Consider not HTML mangling:

var myCode = function () {
    /* Don't use a string for "code" here either,
       then it can be used as a normal function later on. */
}
var myCount = "5"

var $a = $('<a href="#">Browse</a>')
/* Yay, normal event callback function (and closure)! */ 
$a.click(function () {
    myCode()
    alert(myCount)
})

/* In most cases it's like sufficient just to add the element;
   to replace all content, use .empty().append($a) */
$('.cols2.continue').append($a)

Upvotes: 2

Amit Kumar
Amit Kumar

Reputation: 5962

your jquery would be

var myCode = "12345";
var myCount = "5"

$('.continue').html('<a href="#" onClick="test.element=&#39;'+myCode+', '+myCount+' &#39;;test.elemName='+myCode+ ';">Browse</a>');

JsFiddel

Upvotes: 0

martinezjc
martinezjc

Reputation: 3555

Try this:

var myCode = "12345";
var myCount = "5";

$('.cols2.continue').html('<a href="#" onClick="test.element=&#39;'+myCode+',     '+myCount+'&#39;test.elemName='+myCode +';">Browse</a>');

Upvotes: 0

Related Questions