0

Here is my for each in question:

(function() {
    //for legend tags
    const editText = (text) => `<p class="d-inline"><</p><p class="d-inline">${text.replace('<','').replace('>','')}</p><p class="d-inline">></p>`;
    $.ajax({
        type: "GET",
        url: "/clinical/bbr-message-template-legend-list",
        dataType: "json",
        success: function (response){
            var tbody="";
            $.each(response.all_legends, function (key, legend) {
            tbody+='<tr>'+
                        '<td><a href="#" onclick="myFunction()">'+editText(legend.value)+'</a></td>'+
                        '<td>'+legend.text+'</td>'+
                    '</tr>';

            });
            $('#legend-list tbody').html(tbody)
        }
    });

I am trying to get an alert popping whenever I click on the tags below (blue)

1

Attempting to use a function at: <a href="#" onclick="myFunction()">

but whenever I try to use a function inside or outside ajax, my function is not being read:

function myFunction() {
  alert("I am an alert box!");
}

errors:

2

3

How do I get my function to be read?

Error in the console:

message-template:1 Uncaught ReferenceError: myFunction is not defined
    at HTMLAnchorElement.onclick
  • These are "unused code" warnings and not errors. Don't use inline JS (`onclick="..."`) and instead use `.on()` and event delegation (`$().on("click", "a", myFunction)`) – Andreas Sep 17 '21 at 09:17
  • Your text editor just fails to see that `myFunction()` is called from an inline HTML attribute. This function is never explicitely called from your JS code, so it tells you this warning. This should have no impact on the code execution. What happens at runtime? – Jeremy Thille Sep 17 '21 at 09:17
  • Also these are Typescript errors (`myFunction():void`, `ts(6133)`). Is your file extension `.ts` instead of `.js`? If so, why? – Jeremy Thille Sep 17 '21 at 09:19
  • my extension is .js –  Sep 17 '21 at 09:21

0 Answers0