Reputation: 157
I would like to click divs inside an #TAGS1
$(document).on('click','div > #TAGS1',function(){alert('go crazy');});
I tried a variety of things but I cannot seem to make it work. I would like to use .on.
Upvotes: 0
Views: 5006
Reputation: 2171
The jquery method .click() is an shortcut to the click event for .on(), changing your event binding to:
$('#TAGS1 div').click(function(e){
alert('go crazy');
});
Should achieve what you want.
If you really want to use .on()
you can do so using the following:
$(document).on('click', '#TAGS1 div', function(e) {
alert('go crazy');
});
Upvotes: 0
Reputation: 32
the hell?
provide your jquery version next time...
Anyways. your outdated code resembles this of updated and more preferred code.
$("div > #TAGS1").click(function(e){
alert("clicked");
});
if your code is not working still, then you have an issue with your html. div > #tags1 is saying.. Tags1 is a child of division. make sure that is true in your html.
Upvotes: -3
Reputation: 3254
If you have an element with #TAGS1 and you want handle clicks on every div inside of that element than this should work for you.
<section id="TAGS1">
<span>span1</span>
<div>div1</div>
<div>div2</div>
<span>span2</span>
</section>
$(document).on('click','#TAGS1 div',function(){alert('go crazy');});
Upvotes: 1
Reputation: 3691
You can try this
<script type="text/javascript">
$(document).on('click','#TAGS1 > div',function(){
alert('go crazy');
});
</script>
<div id="TAGS1">
<div>Text 1</div>
<div>Text 2</div>
<div>Text 3</div>
</div>
Upvotes: 1