I have a link that is currently a string. It looks like this:
"<h2>New Test</h2><br><a href='/map/create-new?lat=35.7&lng=-83.55'>Create</a>"
The above works, but when I try to insert variables into the spot where 35.7
and -83.55
are then I end up breaking the link and it doesn't work.
I tried like this:
"<h2>New Launch</h2><br><a href='/map/create-new?lat=' event.latlng.lat '&lng=' event.latlng.lng'>Create</a>"
The variables are event.latlng.lat
and event.latlng.lng
.
When I try it like this, then the href ends up only being translated to:
map/create-new?lat=
so I know that something is wrong with my placement of quotes but I'm just not seeing the issue.
EDIT: just for clarification, it must be a string like I have. I am passing this into a component that I did not make and this is how it works.
CodePudding user response:
You can not add or use variables directly in your markup (html) but you can solve it and get desired results by using javascript (code below):
const customLink = document.getElementById("customLink");
var1 = 35.7;
var2 = -83.55;
customLink.href = "/map/create-new?lat=" var1 "&lng=" var2;
<h2>New Test</h2><br><a id="customLink" href=''>Create</a>
CodePudding user response:
By using the right quotes:
var a = 1;
var b = 2;
var mylink = "http://website.com/page.aspx?list=" a "&sublist=" b;
If you start a string with doublequotes, it can be ended with doublequotes and can contain singlequotes, same goes for the other way around.
This answer was found to this question. How to insert javascript variables into a URL
best way to use variables within quotes is like this.
<a href=`/map/create-new?lat= ${event.latlng.lat} &lng=${event.latlng.lng}`>
CodePudding user response:
Use variables like this within a string.
<a href=`/map/create-new?lat= ${event.latlng.lat} &lng=${event.latlng.lng}`>