user3565264
user3565264

Reputation: 157

Jquery click div inside div using on

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

Answers (6)

Sean Quinn
Sean Quinn

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

Ramulis
Ramulis

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

milagvoniduak
milagvoniduak

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.

FIDDLE

<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

Amir Popovich
Amir Popovich

Reputation: 29846

$("#TAGS1 div").click(function() {
    alert('go crazy');
});

Upvotes: 0

Navneil Naicker
Navneil Naicker

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

ryudice
ryudice

Reputation: 37456

Try:

$('#TAGS1 div').on('click',function(){alert('go crazy');});

Upvotes: 3

Related Questions