0

Let's say we have divs with class .row.

Currently my live click function looks like that:

$(".row").live('click',function(event) {

What I want to do is to apply this function only to those divs, which are located inside exact div with class data. I mean

<div class="row">!don't apply!</div>
<div class="data">
   <div class="row">apply1!</div>
   <div class="row">apply2!</div>
</div>

How can I do that?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
heron
  • 3,611
  • 25
  • 80
  • 148

5 Answers5

3

Use jQuery's .on()

$('.data').on('click', '.row', function(){
  // function stuff
});

If the .data is loaded asynchronously too, you can go a "step further"

$('body').on('click', '.data .row', function(){
  // function stuff
});

Just replace 'body' with your wrap selector...

mreq
  • 6,414
  • 4
  • 37
  • 57
  • HTML dynamically changes by Ajax. So I'm using live. Please take a look at this question too http://stackoverflow.com/questions/10427570/delete-divs-by-2-classes-and-node-attribute – heron May 03 '12 at 09:38
  • If `.data` is loaded via AJAX too, check this edit. – mreq May 03 '12 at 09:40
1
$(".data > .row").live('click',function(event) {

});

But Try to avoid live() instead use like following:

$('.data').on('click', '.row', function() {

});

Or you can use delegate()

$(".data").delegate(".row", "click", function(){

});
thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
  • Thx very much for your answer, sir. HTML dynamically changes by Ajax. So I'm using live. Please take a look at this question too http://stackoverflow.com/questions/10427570/delete-divs-by-2-classes-and-node-attribute – heron May 03 '12 at 09:37
1

You could be a bit more specific with your selector i.e.

$(".data > .row").live('click',function(event) {...
phuzi
  • 12,078
  • 3
  • 26
  • 50
  • Please take a look at this question too http://stackoverflow.com/questions/10427570/delete-divs-by-2-classes-and-node-attribute – heron May 03 '12 at 09:37
0
$('.data > .row').live('click',function(event) {

There you go.

The CSS selector > only picks "true" children. No grandchildren allowed.

And do use on() since live() isn't encouraged any longer.

Community
  • 1
  • 1
Robin Castlin
  • 10,956
  • 1
  • 28
  • 44
  • 1
    No, don't use `div.data` instead of `.data`. – mreq May 03 '12 at 09:33
  • Check this answer to my question: http://stackoverflow.com/a/9181607/910868 – mreq May 03 '12 at 09:36
  • Please take a look at this question too http://stackoverflow.com/questions/10427570/delete-divs-by-2-classes-and-node-attribute – heron May 03 '12 at 09:38
  • I find it easier to work with 'div.class' then '.class' because that helps me have a better overlook of what I'm working with. – Robin Castlin May 03 '12 at 09:40
  • I'd consider using comments then... The performance diff is quite large. – mreq May 03 '12 at 09:43
  • Yeah I suppose... I use div.class in css, so I simply find it logical to use it in jQuery aswell. Can't really see myself writing `$('.class');` though. – Robin Castlin May 03 '12 at 09:56
0
$('.data > .row').live('click',function(event) {
Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193