1

I'd like to insert variables to an snippet of html text like

<form action="#" method="post"> <textarea class="form-control" name="comment" id="editComment25"></textarea > <br> <button class="btn btn-primary" id="commentBtnID25">Save Edits</button > <a href="#">cancel</a> </form > 

I tried with an clumsy solution:

var comment_id = $(e.target).attr("id") var editCommentID = "editComment" + comment_id var commentBtnID = "commentBtnID" + comment_id var commentEditForm = '<form action = "#" method = "post"><textarea class="form-control" name ="comment" ' commentEditForm += 'id="' + editCommentID + '"></textarea > <br>'; commentEditForm += '<button class="btn btn-primary" id="' + commentBtnID + '">Save Edits</button > <a href="#">cancel</a></form >' 

Is interpolation possible here as it does in python?

commentEditForm = """" <form action="#" method="post"> <textarea class="form-control" name="comment" id="%s"></textarea > <br> <button class="btn btn-primary" id="%s">Save Edits</button > <a href="#">cancel</a> </form > """ %(editCommentID, commentBtnID) 

1 Answer 1

3

It would probably be easier to use a template literal to define the desired string all at once:

var comment_id = '25'; // $(e.target).attr("id") var commentEditForm = ` <form action="#" method="post"> <textarea class="form-control" name="comment" id="editComment${comment_id}"></textarea > <br> <button class="btn btn-primary" id="commentBtnID${comment_id}">Save Edits</button > <a href="#">cancel</a> </form > `; console.log(commentEditForm);

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.