-1

Anyone knows how to concat in js a string with a integer and don't get the ":

This

"<agm-map latitude=" + response.latitude + " longitude=" + response.longitude + "></agm-map>";

Results on

<agm-map [latitude]="51.678418" [longitude]="7.809007"></agm-map>

But I'm trying to get without the ", as I show bellow

<agm-map [latitude]=51.678418 [longitude]=7.809007></agm-map>

Thanks for any help

Mervin Samy
  • 366
  • 4
  • 13
Alexandre Bento
  • 35
  • 1
  • 10
  • 3
    `"test" + 5 === "test5"` - someone might have a crystal orb and know what you want, but "concat string with integer" does not magically produce quotes. – ASDFGerte Apr 20 '18 at 01:17
  • 3
    It looks like you're trying to produce an HTML snippet. In which case, the browser is likely adding the quotes around the values automatically. – fubar Apr 20 '18 at 01:21
  • fubar, it's exactly that. Do you know how do I insert the text on html without adding the quotes? – Alexandre Bento Apr 20 '18 at 01:25
  • @AlexandreBento Yes, it is exactly that, and you can’t change it. Parsing, then serializing your desired HTML string yields ``, because this is the only valid way to write these attributes. For reference: `new XMLSerializer().serializeToString(new DOMParser().parseFromString('', "text/html"))`. – Sebastian Simon Apr 20 '18 at 01:31
  • 2
    @AlexandreBento - you can't. The browser is going to treat all HTML attributes as a string. You'd need to parse the value in your JS (assuming) back to a float. – fubar Apr 20 '18 at 01:31
  • What do you mean fubar? I'm sorry, but I didn't understood that parse the value back to float – Alexandre Bento Apr 20 '18 at 01:41
  • `` element isn't a standard HTML element. So I assume you're using it with a JavaScript framework (e.g. React, Angular, Vue). That being the case, when you get the element properties, before you use the `latitude` and `longitude`, cast the value from a `string` to a `float`. – fubar Apr 20 '18 at 01:51
  • I'm using Angular, I was searching for a way to use maps on my api, I found that ``, on my problem I use http://freegeoip.net/json/ to get one json with some data about my IP include the latitude and longitude, so I need to use those values to show my location on a map. My code is just that: – Alexandre Bento Apr 20 '18 at 01:57
  • `function getLocation() { $.ajax({ type : 'GET', url : 'http://freegeoip.net/json/' + document.getElementById('domain').value, success : function(response){ var test = ""; document.getElementById('map').innerHTML = test; } }); }` my code is just that... :( – Alexandre Bento Apr 20 '18 at 01:58

1 Answers1

1

Reserved characters in HTML must be replaced with character entities. Reference Link

JSbin Link

Code:

function myFunction() {
  //replace lat & lng value with the response
  var latitude = 51.678418;
  var longitude = 7.809007;
  document.getElementById('demo').innerHTML="&lt;agm-map [latitude=]" + latitude + " [longitude]=" + longitude + "&gt;&lt;/agm-map&gt;";
}
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>

<body>
  <div>
    <button onclick="myFunction()">Click Me</button>
    <p id="demo">Change text</p>
  </div>
</body>

</html>
Community
  • 1
  • 1
Ricky Cuarez
  • 708
  • 4
  • 14