Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interpolation in JavaScript

I have this jQuery code

$("#selector").html('<a href=url>text</a>');

where url and text are JavaScript variables. How would I evaluate them inside quotes?

like image 457
Justin Meltzer Avatar asked Jul 11 '11 00:07

Justin Meltzer


2 Answers

You just concatenate the strings together with +:

$('#selector').html('<a href='+url+'>'+text+'</a>');
like image 116
James Montagne Avatar answered Sep 27 '22 18:09

James Montagne


While @kingjiv's answer is absolutely right, if you'll be doing a lot of templating with jQuery it might be worth checking out the tmpl* plugin to help keep things organized.

*note: This plugin never made it past beta, and is no longer being maintained, the link above is for archival purposes only.

like image 36
fncomp Avatar answered Sep 27 '22 19:09

fncomp