1

I have search the previous question but couldn't find the answer.

Dynamically generated html:

<a class="finddiv" id="#div_0">date1</a>
<a class="finddiv" id="#div_1">date1</a>
<a class="finddiv" id="#div_2">date1</a>

hidden div which i have to open- on cllick of above link:

<div id="div_0" style="display:none;"></div>

<div id="div_1" style="display:none;"></div>

<div id="div_2" style="display:none;"></div>

I want to open the div with id div_o by clicking of anchor tag with class name find, I am using the following jquery:

$(document).ready(function() {
    $('a.find').delegate(this, 'click', function() {
        var ids = $(this).attr('id');
        $(ids).slideToggle();
        return false;
    });
    return false;
});

But above function is running as many time as the number of elements in the class "find". In above case 3 times. I just want to run the function once. I have also used "one", "live" properties in jquery but nothing has happened. Since the content is dynamic I can not use the id attribute to call jquery function.

Adam Flanagan
  • 3,052
  • 23
  • 31
Sumit Neema
  • 462
  • 3
  • 18

6 Answers6

1

You have your delegate mixed up:

$(document).delegate('a.finddiv', 'click', function(){
    $(this.id).slideToggle();
    return false;
});

Demo: http://jsfiddle.net/AndyE/AZCky/

Attaching like this also means you don't need to place it inside $(document).ready().

Andy E
  • 338,112
  • 86
  • 474
  • 445
0

You don't need to use delegate here, a simple .click will work just fine.

$(document).ready(function(){
    $('a.finddiv').click(function(){
        var ids = $(this).attr('id');
        $(ids).slideToggle();
        return false;
    });
});

Demo: http://jsfiddle.net/djweQ/

You only need to use .delegate if the elements are added to the DOM at a later point. Try it like this (you can change 'body' if you want, as long as it's a parent of the <a> tags):

$('body').delegate('a.finddiv', 'click', function() {
    var ids = $(this).attr('id');
    $(ids).slideToggle();
    return false;
});
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
0

In your markup the class name is not find but it is finddiv so you should use finddiv in the javascript code to find the anchor element. Also the way you are using delegate is wrong. Since the anchor elements are generated dynamically you should have a delegate on the document object. Try this.

$(document).ready(function(){
        $(document).delegate('a.finddiv', 'click', function(){
           var ids = $(this).attr('id');
           $(ids).slideToggle();
           return false;
        });
});    

As a side note. Instead of using id as attribute in the anchor element to maintain the id of the div to slide toggle you can use href attribute and get the href attribute inside click handler.

ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
  • yes i have used finddiv but it is running three times means for every element in that class the function is running i just want to call it for the current cliked element in the class – Sumit Neema Dec 08 '11 at 14:53
0

Try this:

$("body").delegate(".finddiv", "click", function(e) {
    e.preventDefault();
    $($(this).attr('id')).slideToggle();
});

If your .finddiv elements have a direct parent into which they are dynamically apended, use the selector for that instead of body as you will get much better performance.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
0

First of all your initial mark-up has what appears to be duplicate id's. The id attribute has to be unique. What I'd suggest would be to use the following:

<!---------dynamicaly generated html------------>
<a class="finddiv" id="a_0">
date1
</a>
<a class="finddiv" id="a_1">
date1
</a>
<a class="finddiv" id="a_2">
date1
</a>

<!---------hidden div which i have to open- on cllick of above link----->
<div id="a_0_div" style="display:none;">  
</div>
<div id="a_1_div" style="display:none;">
</div>
<div id="a_2_div" style="display:none;">
</div>

And then:

$(document).ready(function(){
    $('a.finddiv').click(function(event) {
        var divId = $(this).attr('id') + '_div';
        $(divId).slideToggle();
        event.preventDefault();
    });
});

As an alternative, each <a> tag doesn't need an id attribute, you could use:

<!---------dynamicaly generated html------------>
<a class="finddiv" data-open="div0">
date1
</a>
<a class="finddiv" data-open="div1">
date1
</a>
<a class="finddiv" data-open="div2">
date1
</a>

<!---------hidden div which i have to open- on cllick of above link----->
<div id="div0" style="display:none;">  
</div>
<div id="div1" style="display:none;">
</div>
<div id="div2" style="display:none;">
</div>

and

$(document).ready(function(){
    $('a.finddiv').click(function(event) {
        $($(this).data('open')).slideToggle();
        event.preventDefault();
    });
});

Though this will only work in JQuery 1.7 onwards

Phil Peace
  • 733
  • 1
  • 7
  • 20
-1
$('.finddiv').click(function(){

  $($(this.attr('id')).show());

}); 
shodanex
  • 14,975
  • 11
  • 57
  • 91
Lee
  • 10,496
  • 4
  • 37
  • 45