Onclick inside innerHTML

hello,
what is wrong whis this code?

<div id=“Mydiv”>anything</div>
<script>
function test(x){alert(x)};
var col=document.getElementById(‘Mydiv’);
col.innerHTML=‘<div onclick=“alert(2);test(123);”>Text</div>’;
</script>

notes:
1-alert(2); works , but test(123) not working , test(44) is working(outside innerHTML)
2- i want to use parameters inside test() functions i.e: test(123)

Sounds like you are trying to call the function before the DOM is ready which would caused an undefined function error since the DOM doesn’t know about the function yet when you embed the new HTML, see the below which should solve the issue.

window.onload = function() {
    function test(x){alert(x);};
    var col=document.getElementById('Mydiv');
    col.innerHTML='<div onclick="alert(2);test(123);">Text</div>';
};

didn’n work

Is there a demo page that I can look at as without one its extremely hard to know whats going wrong.

you can copy the code and make an html page

For me, this works as expected.
Maybe you have a typo somewhere?

<!DOCTYPE HTML>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Unbenanntes Dokument</title>
  </head>

  <body>
    <div id="Mydiv">anything</div>

    <script>
     function test(x){alert(x)};
     var col=document.getElementById('Mydiv');
     col.innerHTML='<div onclick="alert(2);test(123);">Text</div>';
    </script>
  </body>
</html>

Wrapping everything in window.onload also worked.