dcsimg
www.webdeveloper.com
Results 1 to 4 of 4

Thread: Onclick inside innerHTML

  1. #1
    Join Date
    Mar 2013
    Posts
    2

    resolved Onclick inside innerHTML

    hello,
    what is wrong whis this code?

    <div onclick="test(44)">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)
    3-

  2. #2
    Join Date
    Feb 2012
    Location
    youTUBE
    Posts
    234

    Making it function.

    <?php
    $str3=<<< ENDd
    <html>
    <head>
    <script type="text/javascript">
    function test(x){alert(x)};
    </script>
    <body>
    <div id="MYdiv"> this id My div</div>
    <div onclick="test(44)">anything</div>
    <script type="text/javascript">
    var col=document.getElementById('Mydiv');
    col.innerHTML='<div onclick="alert(2);test(123);">Text</div>';
    </script>
    </body>
    </html>
    ENDd;
    echo $str3 ;
    The above version works.

  3. #3
    Join Date
    Mar 2013
    Posts
    2
    Quote Originally Posted by WyCnet View Post
    The above version works.
    No , it didnt work with me ,
    it fired test(44) , but what i need is to fire test(123) that's inside innerHTML

  4. #4
    Join Date
    Feb 2012
    Location
    youTUBE
    Posts
    234
    If you notice, you have shown nothing with id "Mydiv".
    <div onclick="test(44)">anything</div>
    < script>
    function test(x){alert(x)};
    var col=document.getElementById('Mydiv');
    col.innerHTML='<div onclick="alert(2);test(123);">Text</div>';
    < /script>
    therefore : add the id = "Mydiv", as shown below:

    <html>
    <head>
    <script type="text/javascript">
    function test(x){alert(x)};
    </script>
    <body>
    <div id="MYdiv" onclick="test(44)">anything</div>
    <script type="text/javascript">
    var col=document.getElementById('Mydiv');
    col.innerHTML='<div onclick="alert(2);test(123);">Text</div>';
    </script>
    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles