Ali Najm Ali Najm - 6 days ago 6
Javascript Question

How to add var inside new link herf using jquery?

i try to add var inside herf of a new link like this:

$(document).ready(function(){
var getuser = $( ".username span" ).text().replace("@", "");
var getnickname = $( ".nickname span" ).text();
$( ".go-premium" ).html('<a href="http://www.domain.com/test.html?username=getuser&name=getnickname">Go premium!</a>');
});


HTML code :

<div class='nickname'><span>Ali</span></div>
<div class='username'><span>@AliUser</span></div>

<div class='go-premium'></div>


I cant figure out what is wrong.

Thanks.

Answer

Just put your vars outside of string double quotation:

$(document).ready(function(){
   var getuser = $( ".username span" ).text().replace("@", "");
   var getnickname = $( ".nickname span" ).text();
   $( ".go-premium" ).html('<a href="http://www.domain.com/test.html?username=' + getuser + '&name=' + getnickname + '">Go premium!</a>');
});
Comments