SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up 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)

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,197
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.

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

  3. #3
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    didn'n work

    Quote Originally Posted by chris.upjohn View Post
    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.

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

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,197
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Is there a demo page that I can look at as without one its extremely hard to know whats going wrong.

  5. #5
    SitePoint Member
    Join Date
    Mar 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can copy the code and make an html page

    Quote Originally Posted by chris.upjohn View Post
    Is there a demo page that I can look at as without one its extremely hard to know whats going wrong.

  6. #6
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,008
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    For me, this works as expected.
    Maybe you have a typo somewhere?

    HTML Code:
    <!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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •