Search code examples
htmlhref

How to convert this HTML to a string?


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.


Solution

  • Nothing in the other answers worked for me. I ended up having to install jquery and then used it like this:

    var link = jQuery("<a href='#'>Create New</a>").click(function () {
        self.onClickCreateNewLaunch(event.latlng.lat, event.latlng.lng);
    })[0];
    

    And then just navigated to the page using the function.