1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

how to trigger my script from innerHTML

Discussion in 'HTML & JavaScript' started by sapo, Sep 20, 2012.

  1. sapo

    sapo Power Member

    Joined:
    Feb 25, 2008
    Messages:
    510
    Likes Received:
    281
    okay so I have a locker that get triggered through an ID. Works fine in regular HTML but once I place the trigger link in innerHTML it will not trigger my locker.

    that normal code that triggers the locker is (id that trigger is "pop")
    Code:
    <a id="pop" href="javascript:void()">How the Locker POPS Normally</a>
    and this is the code I use on innerHTML, but it does not trigger the locker

    Code:
    <script type="text/javascript">
    function ChangeStyle()
    {
      document.getElementById("p12").innerHTML = "<div class=\"button\"><a id=\"pop\" href=\"javascript:void()\">Second Button - innerHTML (this Should trigger the Locker)</a></div>";
    }
    
    </script>
    	    <span id="p12">
     <div class="button"><a href="javascript:void()" onclick="ChangeStyle();">First Button (this should trigger innerHTML) </a></div></span>
    Now this doesnt work, I know its something to do how innerHTML handles this or something, Ive been reading alot and trying alot of things Like, trying to trigger the fucntion() , trying onclick, a bunch but its out of my element to figure out at this time. I hate asking question but when you need help you need help.

    I have set up a live site so you can see the code and everything in its entirety so you can properly evaluate it.

    Code:
    http://bit.ly/PWRNZG
    Thanks any help is appreciated.
     
    Last edited: Sep 20, 2012
  2. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,468
    Likes Received:
    10,147
    It does not work because there is a javascript error after you click the first link.

    Code:
    Syntax error at line 1 while loading: expected expression, got ')'void()
    
     
  3. Zapdos

    Zapdos Power Member

    Joined:
    Oct 22, 2011
    Messages:
    597
    Likes Received:
    708
    Location:
    Eastern North Carolina
    Why not use jQuery and make it simple?

    Code:
    // Insert jquery
    <script src="/jquery.js" type="text/javascript"></script>
    
    <span id="p12">
        <div class="button">
            <a id="initpop" href="#_">First Button (this should trigger innerHTML) </a>
        </div>
    </span>
    
    <script type="text/javascript">
    $('#initpop').click(function() {
      $('#p12').html('<div class="button"><a id="pop" href="#_">Second Button - innerHTML (this Should trigger the Locker)</a></div>');
    });
    </script>
    
     
    Last edited: Sep 20, 2012
  4. sapo

    sapo Power Member

    Joined:
    Feb 25, 2008
    Messages:
    510
    Likes Received:
    281
    added the "0" in void(0) still doesnt work

    I just tested that and changed the trigger to pop2 on this and it still doesnT work. It goes to the second link but doesnt trigger the locker

    I added the test page i used it on so you can see it.
    Code:
    http://bit.ly/RyNCFl