-1

I have this code from my JS File:

var formStr = "<h5>How many books?:</h5><input type='number' id='bookinput'
value='' /><input type='button' value='submit' onclick='addbook();' />"
infowindow = new google.maps.InfoWindow();
infowindow.setContent(formStr);
infowindow.setPosition(location);
infowindow.open(map);

JS variable formStr has html code as you can see above. I want to be able to put JS variables into this without moving the HTML out of my JS file. Something like:

var objectType='book';
var formStr = "<h5>How many *objectType*?:</h5><input type='number' id='bookinput' value='' /><input type='button' value='submit' onclick='addbook();' />"
0

3 Answers 3

1

Just concatenate. var formStr = "<h5>How many " + objectType + "?:</h5>" and so on.

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

Comments

1

This is simple. Using your example:

var objectType='book';
var formStr = "<h5>How many " + objectType + "?:</h5><input type='number' id='bookinput' value='' /><input type='button' value='submit' onclick='addbook();' />"

Comments

1

--> Replace :

formStr.replace("*objectType*", "books")

--> ES5 concatenation : see other answers

--> ES6 string interpolation :

let formStr = `<h5>How many ${objectType}?:</h5>`

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.