8

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?

1
  • I created an open source project for interpolation you can try. github.com/zsong/Kiwi Thank you. Commented Feb 15, 2013 at 18:27

3 Answers 3

11

You just concatenate the strings together with +:

$('#selector').html('<a href='+url+'>'+text+'</a>');
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

0

For anyone landing here post 2015: use ES6 template literals. These are string literals, enclosed in backticks, which allow embedded expressions.

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

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.