0

I have recently converted my code into an asp.net format but am getting an this Uncaught TypeError with one of my JS functions. I have tested and it works when its not in the asp.net format.

The error comes when i interact with a button that i supposed to close a message. I have double checked syntax but still cant find the error.

This is the button -

<div id="main_modal" class="modal">
    <button type="button" value="Exit Modal" id="exit_modal" onclick="exit_modal()">X</button>
</div>

This is the function it calls onclick -

function exit_modal(){
    document.getElementById("main_modal").style.visibility = "hidden";
}

This is the error received in Chrome dev tools when button is clicked -

Uncaught TypeError: exit_modal is not a function at HTMLButtonElement.onclick

I am new to using asp.net so any help would be very appreciated. Thanks in advance.

Clifferto
  • 57
  • 8
  • 1
    Sounds like your `exit_modal` function is [not in scope](https://stackoverflow.com/a/59539045) from the inline handler (which can happen if the function is defined in another function rather than on the top level). Best solution would be to attach the listener properly via Javascript instead: `document.querySelector('#exit-modal').addEventListener('click', () => { /* code */ });` – CertainPerformance Feb 11 '20 at 09:53
  • Ive just implemented what you suggested and its worked. Thank you for your help wasn't sure if this was a duplicate post. – Clifferto Feb 11 '20 at 10:08

0 Answers0