1

I have this code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>


var newPoint = "<div class='item-point' data-top='" + cY + "' data-left='" + cX + "' id='point" + random + "'>" +
"<div>" +
"<a onclick='test(1);' target='_blank' href='" + url + "'  class='toggle tooltips' title='" + description + "'  data-placement='top' data-html='true' rel='tooltip'><span class='pointFormat'>" + pointCount + "</span></a>" +
"</div>" +
"</div>";

$(".scalize").append(newPoint);


<div class="scalize"></div>

function test(variable) {
   alert(variable);
}

and it's not working. I have error: ReferenceError: Can't find variable: variable.

I have error when I click on link (a href)

How to repair it?

1
  • 1
    You can't mix javascript and HML like that. Commented Feb 2, 2019 at 9:10

1 Answer 1

1

You need to move all your javascript inside the <script> tag and keep the <div class="scalize"></div> HTML out of it. Like this:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="scalize"></div>

<script>
  let cY, cX, random, url, description, pointCount = "Some text";

  var newPoint = "<div class='item-point' data-top='" + cY + "' data-left='" + cX + "' id='point" + random + "'>" +
    "<div>" +
    "<a onclick='test(1);' target='_blank' href='" + url + "'  class='toggle tooltips' title='" + description + "'  data-placement='top' data-html='true' rel='tooltip'><span class='pointFormat'>" + pointCount + "</span></a>" +
    "</div>" +
    "</div>";

  $(".scalize").append(newPoint);

  function test(variable) {
    alert(variable);
  }
</script>

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.