Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. <button onclick="test()">Button 1</button>
  2. <button onclick="test()">Button 2</button>
  3. <button onclick="test()">Button 3</button>
  4.  
  5. function test()
  6. {
  7. var button_name = this.html;
  8. console.log("Im button "+ button_name);
  9. }
  10.  
  11. <button onclick="test(this)">Button 1</button>
  12.  
  13. function test(elem){
  14. var button_name = elem.textContent;
  15. }
  16.  
  17. <button onclick="test(this)">Button 1</button>
  18.  
  19. var buttons = document.querySelectorAll("selector-for-the-buttons");
  20. Array.prototype.forEach.call(buttons, function(btn) {
  21. btn.addEventListener("click", handler, false);
  22. });
  23. function handler() {
  24. // Use `this` here
  25. }
  26.  
  27. $("selector-for-the-buttons").on("click", function() {
  28. // Use `this` here
  29. });
  30.  
  31. document.querySelector("selector-for-the-container").addEventListener("click", function(e) {
  32. // Use `e.target` here
  33. }, false);
  34.  
  35. $("selector-for-the-container").on("click", "button", function() {
  36. // Use `this` here (note this is different from the DOM version above)
  37. });
  38.  
  39. <button onclick="test(this)">1</button>
  40. <button onclick="test(this)">2</button>
  41. <button onclick="test(this)">3</button>
  42.  
  43. <script>
  44. function test(t)
  45. {
  46. console.log(t);
  47. }
  48. </script>
  49.  
  50. <button onclick="test(this)">1</button>
  51. <button onclick="test(this)">2</button>
  52. <button onclick="test(this)">3</button>
  53.  
  54. <script>
  55. function test(button)
  56. {
  57. var button_name = $(button).html();
  58. alert("Im button "+ button_name);
  59. }
  60. </script>
  61.  
  62. <button onclick="test(this.id)" id="button1">1</button>
  63. <button onclick="test(this.id)" id="button2">2</button>
  64. <button onclick="test(this.id)" id="button3">3</button>
  65.  
  66. <script>
  67. function test(id)
  68. {
  69. var button_name = id;
  70. alert("Im button name is : "+ button_name);
  71. console.log("Im button name is :"+ button_name);
  72. }
  73. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement