0

I've got a script which gets a bunch of information from an api and then lists it on my site. I then have a function, newLink(z) which is supposed to get the information from the new elements created and do stuff with it, to simplify it I'm just trying to console.log whatever it says below. Any number that I put into the brackets of the function, in this case 76561198008132325, has the last digit replaced with a 0 for whatever reason, resulting in the console logging '76561198008132320'. I've been scratching my head on this for a good half hour now and I literally can not figure out what is causing this.

    var searchRequest = "https://api.roleplay.co.uk/v1/search/player?name=ant";
    var searchData = "";
    
    function success(data) {
      for (i = 0; i < data['length']; i++) {
          

          
          document.getElementById("searchResults").innerHTML += "<div class='result' onclick='newLink(76561198008132325)'>new link</div>";
      }
    }

function newLink(z) {
    console.log(z);
}


    $.ajax({
      dataType: 'json',
      url: searchRequest,
      data: searchData,
      success: success
    });
<div class="results" id="searchResults">
        </div>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  • Maybe the value is too big...if a string is ok...put it in quotes?...right now it's treated as a number – Ctznkane525 Dec 29 '17 at 00:36
  • Change it to quoted string. You are running into javascript max integer size – charlietfl Dec 29 '17 at 00:37
  • It will have to be treated as a number though - this is a much more simplified version of my script, in reality I'm taking elements from the new elements being created which are numbers –  Dec 29 '17 at 00:38
  • The output is "new link", however if you clickk on that text it should display '76561198008132325' but it instead displays 76561198008132320 on the console –  Dec 29 '17 at 00:39
  • Check out this answer https://stackoverflow.com/questions/1379934/large-numbers-erroneously-rounded-in-javascript – explains what is happening here. Passing a string as the answer posted by Mamun suggests is how i would proceed. – luckyape Dec 29 '17 at 00:40

1 Answers1

0

This is because you are passing the argument to the function as Number. Pass the argument value as string.

var searchRequest = "https://api.roleplay.co.uk/v1/search/player?name=ant";
var searchData = "";

function success(data) {
  for (i = 0; i < data['length']; i++) {
  document.getElementById("searchResults").innerHTML += "<div class='result' onclick='newLink(\"76561198008132325\")'>new link</div>";
  }
}

function newLink(z) {
    console.log(z);
}

$.ajax({
  dataType: 'json',
  url: searchRequest,
  data: searchData,
  success: success
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="results" id="searchResults">
</div>
Mamun
  • 66,969
  • 9
  • 47
  • 59