javascript - <button> JQuery onclick works only once -


i have button onclick event works fine here:

// create function remove "popup-open" classes  // function used in onclick attribute  (function( $ ){     $.fn.popupclose = function() {       $( "body" ).removeclass( "popup-open" )       $( ".overlay_btn" ).removeclass("popup-open");        return this;     };   })( jquery );    // if <button> exists  // onclick read button id value  // add "popup-open" class span idvalue class, fadein effect    if ( $( "button.popup" ).length )  {    $("button.popup").click( function()      {       var btnid = $(this).attr('id');      $( "body" ).hide().addclass( "popup-open" ).fadein(100);      $( "."+ btnid ).hide().addclass( "popup-open" ).fadein(200);      }   );  }    if ( $( "button.link" ).length )  {    $("button.link").click( function()      {       var btnformaction = $(this).attr('formaction');      var btntarget = $(this).attr('formtarget');      //alert(btnformaction);      window.open(btnformaction, btntarget);      }     );  }
button {    padding: 10px;    font-size: 1.5rem;    background-color: #d5d5d5;    border: 3px solid #ddd;    margin: 15px;    box-shadow: 0px 0px 15px #888888;    cursor: pointer;  }    button:hover {    box-shadow: 0px 0px 10px #888888;  }    body:after {      content: "";      display: none;      position: fixed; /* absolute */       top: 0;      left: 0;      height: 100%;      width: 100%;      z-index: 10;    background: rgba(0,0,0,0.6);  }    .overlay_btn {    display: none;    padding: 10px;      width: 300px;      height: 200px;      position: fixed;      top: 50%;       left: 50%;      margin-top: -100px;      margin-left: -150px;      background-color: #fff;      border-radius: 5px;      text-align: center;      z-index: 11; /* 1px higher overlay layer */  }    body.popup-open:after, .popup-open {    display: block;  }
<html>    <head>      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>    </head>        <body>      <!--create button unique id* -->      <button id="btn-popup01" class="popup">popup button 1</button>      <!-- create span button#idvalue class value-->      <span class="overlay_btn btn-popup01"><a href="#" onclick="$(this).popupclose();">close</a>      <h3>your content title 1</h3>        <p>your content 1</p>      </span>            <!--create button unique id* -->      <button id="btn-popup02" class="popup">popup button 2</button>      <!-- create span button#idvalue class value-->      <span class="overlay_btn btn-popup02"><a href="#" onclick="$(this).popupclose();">close</a>      <h3>your content title 2</h3>        <p>your content 2</p>      </span>            <!--create button unique id* -->      <button id="btn-link01" class="link" formaction="http://s.emp.re/1kazexz" formtarget="_blank">link button 3</button>                  <p>here, have js-less equivalent, more css (less): <a href="http://codepen.io/maccadb7/pen/nbheg" target="_blank">http://codepen.io/maccadb7/pen/nbheg</a></p>                </body>  </html>

using same code in project: http://kine.sarabernaert.be/contact/

button 'maak afspraak' gives popup on popup there's button 'ga naar aanmelden' links outside link.

i can't working link. when click, nothing happens. in demo setup, same code working well.

any hints? don't see i'm doing wrong.

thx!

replace .click() method .on() method , place them inside $(document).ready @ bottom of page

in case, instead of if ( $( "button.link" ).length ) can use below.

$("body").on('click', 'button.link', function(){      var btnformaction = $(this).attr('formaction');     var btntarget = $(this).attr('formtarget');     window.open(btnformaction, btntarget);         }); 

overall, scripts should this

$(document).ready(function(){     $("body").on("click", "button.popup", function(){        var btnid = $(this).attr('id');       $( "body" ).hide().addclass( "popup-open" ).fadein(100);       $( "."+ btnid ).hide().addclass( "popup-open" ).fadein(200);     }).on('click', 'button.link', function(){        var btnformaction = $(this).attr('formaction');       var btntarget = $(this).attr('formtarget');       window.open(btnformaction, btntarget);             });      $.fn.popupclose = function() {       $( "body" ).removeclass( "popup-open" )       $( ".overlay_btn" ).removeclass("popup-open");       return this;     }; }); 

i tested on site , seems working after that. got redirected login page guess.

hope helps.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -