javascript - fadeToggle on multiple ids errors -
today come issue jquery / javascript , .fadetoggle();
.
so want p
elements id.
please see here:
$("#here").click(function(){ $(".overlay, .popup").fadetoggle(); }); $("#there").click(function(){ $(".overlay, .popup2").fadetoggle(); });
here jsfiddle:
i want, ideally, have several paragraphs individual ids can add pop differing content!
i'd use custom data attribute this. way can reuse same function no matter how many elements have...
$('.clickable').click(function() { var popuptarget = $(this).data('popup'); $('.overlay, '+popuptarget).fadetoggle(); }); /* click overlay close... */ $('.overlay').click(function() { $('div').fadeout(); });
html, body { height: 100%; } .overlay { position:absolute; display:none; /* color alpha transparency */ background-color: rgba(0, 0, 0, 0.7); /* stretch screen edges */ top: 0; left: 0; bottom: 0; right: 0; } .popup, .popup2 { position: absolute; width: 300px; height: 150px; display: none; background-color: white; text-align: center; /* center ? */ top: 50%; left: 50%; margin-left: -150px; margin-top: -75px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <div class="overlay"></div> <div class="popup">some popup text</div> <div class="popup2">hello world!!</div> <!-- add class each element , data attribute identify target element... --> <p id="here" class="clickable" data-popup=".popup">click 1</p> <p id="there" class="clickable" data-popup=".popup2">click 2</p>
Comments
Post a Comment