JavaScript How do I pass a variable to inline javascript using EJS templating engine? php pass variable to inline function,javascript pass variable to

I've created a html list of buttons/text from an array called items in an .EJS template. How do I pass the specific item's id (item.id) to the button's function so I can send the right data to my api? Thanks.

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Menu</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    <script type="text/javascript">

    function print(id) {
        $.ajax({
          url: "https://www.example.com/api/1/print",
          type: "POST",
          data: {
            "item_id": id
          },
          dataType: "json",
          success: function (result) {
            alert(result);
          },
          error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
          }
        });
      };
    </script>
  </head>
  <body>
    <h2>Menu</h2>
    <ul>
        <% for(item of items) { %>
          <li>
            <button onclick="print(item.id)">PRINT</button>
            <%= item.name %> - <%= item.id %>
          </li>
        <% } %>
    </ul>
  </body>
</html>
Answer:1

I'm trying to count the number of "file" in a JSON object recursively. Here is my function, function totalReports(object, count) { count = count || 0; var key; for(key in object) { ...

I'm trying to count the number of "file" in a JSON object recursively. Here is my function, function totalReports(object, count) { count = count || 0; var key; for(key in object) { ...

I am trying to create a filtering for a Stapes object in RactiveJS, but it seem the two way binding is not responding correctly to the update. I can't work out what is going wrong where as I thought ...

I am trying to create a filtering for a Stapes object in RactiveJS, but it seem the two way binding is not responding correctly to the update. I can't work out what is going wrong where as I thought ...

I have a test.txt file that has two columns of data (x's and y's), eg: #test.txt 1 23 2 234 4 52 43 5 3 35 And a python program that reads in these values and stores them in x and y as so: #test.py ...

I have a test.txt file that has two columns of data (x's and y's), eg: #test.txt 1 23 2 234 4 52 43 5 3 35 And a python program that reads in these values and stores them in x and y as so: #test.py ...

  1. transfer array from php to javascript
  2. transfer array in java

I'm currently working on something that requires me to put the value of an input field as an object item. var test = { item1:{ subitem1: "", <------- This is the one I'm trying to fill ...

I'm currently working on something that requires me to put the value of an input field as an object item. var test = { item1:{ subitem1: "", <------- This is the one I'm trying to fill ...