0

I have a button that I am appending to the page. When that button is clicked, it should run some code.

Instead of triggering only when that button is clicked, any clicks on the page triggers it.

I've been able to replicate the issue with only a few lines of code, see the JSFiddle linked.

var myButton = '<div id="myButton">Do Not Click Me</div>';
$('#parent').html(myButton);

jQuery(document).on('click', jQuery('#myButton'), function (e) {
    alert('marco');
});

https://jsfiddle.net/aj4u1y9n/

How can I make the alert run only when the button is clicked?

4
  • 1
    .on('click', '#myButton', function (e) {... Commented Sep 9, 2016 at 19:27
  • Why do you switch between jQuery and $? Commented Sep 9, 2016 at 19:29
  • @j08691 In my actual code I have to use jQuery to prevent a conflict with another library. Commented Sep 9, 2016 at 19:30
  • It seems some jack@ss thinks that our jQuery stinks, I'm +1 everyone. Commented Sep 9, 2016 at 19:43

2 Answers 2

1

Don't use a jQuery selector here:

jQuery(document).on('click', jQuery('#myButton'), function (e)

Instead, do:

$(document).on('click', '#myButton', function (e)

Example:

var myButton = '<div id="myButton">Do Not Click Me</div>';
$('#parent').html(myButton);

$(document).on('click', '#myButton', function(e) {
  alert('marco');
});
#myButton {
  background: #8888ff;
  padding: 1em;
  width: 4em;
  margin-left: 4em;
  margin-top: 4em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="parent">
</div>

By the way, your code could be simplified to this:

var myButton = '<div id="myButton">Do Not Click Me</div>';
$('#parent').html(myButton);

$('#myButton').click(function() {
  alert('marco');
});
#myButton {
  background: #8888ff;
  padding: 1em;
  width: 4em;
  margin-left: 4em;
  margin-top: 4em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="parent">
</div>

BTW, don't switch between the selectors jQuery and $, use one or the other.

Sign up to request clarification or add additional context in comments.

1 Comment

It appeared like somebody downvoted the question and all three answers at once.
0

The way you wrote the click listener, you actually listen for clicks on the whole document. Have a look at the jQuery API:

for more information.

You can achieve what you wish like that:

var $myButton = $('<div id="myButton">Do Not Click Me</div>');
$myButton.click(function (e) {
    alert('marco');
});
$('#parent').append($myButton);

Explanation: You can add the click listener before you insert the element into the DOM. Searching it again in the DOM for adding a click handler is just unnecessary.

Fiddle: https://jsfiddle.net/bkbczLug/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.