Skip to content

Instantly share code, notes, and snippets.

@boppy
Forked from ArunHub/alert-buttons.html
Last active June 12, 2016 12:23
Show Gist options
  • Save boppy/ad8b87be2868b273ce8f3a898bf85df5 to your computer and use it in GitHub Desktop.
Save boppy/ad8b87be2868b273ce8f3a898bf85df5 to your computer and use it in GitHub Desktop.
alert the buttons text //source https://jsbin.com/fawepetaha
<div class="btns">
<p>Write JavaScript (using jQuery is fine) so that clicking on any button will cause an alert to popup with the text of the button that was clicked.</p>
<p>Make sure your solution is as efficient as possible even if there are a lot more <code>button.btn</code>'s added on the page. Do not use unnecessary event listeners.</p>
<button class="btn">c</button>
<button class="btn">l</button>
<button class="btn">o</button>
<button class="btn">s</button>
<button class="btn">e</button>
<button class="btn">.</button>
<button class="btn">i</button>
<button class="btn">o</button>
</div>
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<script id="jsbin-javascript">
// Will even work with buttons inserted after page rendering completed. ;)
$('div.btns').mouseup(function(e){
$(e.target).is('button.btn') && alert(e.target.innerText);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment