-2

I have following code

<div id = "fa10_holder">
<div id = "b-1"></div>
</div>

I am adding this dynamically in a div that holds all of these type of divs .. lets say if I add another div it would be like this

<div id = "fa11_holder">
<div id = "b-2"></div>
</div>

Now the problem is when I click the div with id of b-1 or b-2 and so on I want the id of its parent like fa10_holder and the id of the clicked div aswell... can any one help me ??

Uzumaki Naruto
  • 753
  • 1
  • 9
  • 27

2 Answers2

1
var thisid = this.id;
var parrentid = $(this).parent().attr('id');
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

Like this working demo another way.

APIs:

I have added extra class, in case you have many div you can have a blanket click via class. :)

Extra: When to access the attribute (vs the property)?

code

$(document).ready(function () {
    $(".hulk").click(function () {
        alert($(this).parents('div').attr('id'));
        alert($(this).attr('id'));
    });
});

html

<div id = "fa11_holder"> parent
<div id = "b-2" class="hulk">hulk</div>
</div>
Community
  • 1
  • 1
Tats_innit
  • 33,991
  • 10
  • 71
  • 77
  • Excellent !! now there is only one thing left what if there is a textarea with an id = "some_id" below the div id = "b-2" how can I get the id of the textarea as well thanks in advance :) – Uzumaki Naruto Aug 26 '14 at 08:08
  • Glad it helped you @UzumakiNaruto `:)` , Try `.next` Api with $(this) element, also checkout how the .next actually works form query documentation. – Tats_innit Aug 26 '14 at 10:51