1

I am implementing a discussion panel in ASP.net, in which I am assigning the comment id to the comments.

I want to call the jQuery click function on every comment click event. How I call the click function because in comment div I am assigning the ids 1,2,3.... and I don not know the range so I can not use $("#1 || #2").click().

user2428118
  • 7,935
  • 4
  • 45
  • 72
Billz
  • 1,067
  • 6
  • 25
  • 57

4 Answers4

5

Assign the same class to each of the comment divs and then do $(".commentdiv").click

jQuery is awesome like this.

If you need to know the id of the element that was clicked later, you can get it through this.id.

$(".commentdiv").click(function() {
   alert(this.id);
});
Sprachprofi
  • 1,229
  • 12
  • 24
1

Use class selector

$(".panel").click(clickfunction);

function clickfunction()
{
    //do something
    id = $(this).attr('id');
    alert(id);
}
Govind Malviya
  • 13,627
  • 17
  • 68
  • 94
1

In html add many divs like this

<div id="div1" onclick="Myfun(this)">
<div id="div2" onclick="Myfun(this)">

In javascript

function MyFun(currentDiv)
{
alert(currentDiv.id);
}
Adil
  • 146,340
  • 25
  • 209
  • 204
1

Assigning same class is the answer, If you want to count the number of divs then try this

var top_level_div = document.getElementById('id_of_first_div');

var count = top_level_div.getElementsByTagName('div').length;

The getElementsByTagName() is not only a document method, but one that can run on any DOM element.

element.getElementsByTagName is similar to document.getElementsByTagName, except that its search is restricted to those elements which are descendants of the specified element

I found it here

Community
  • 1
  • 1
Chaithanya
  • 608
  • 1
  • 8
  • 29