tags:

views:

46

answers:

2

Im trying now a long time to access the content of a div by click it.

The structure is like this:

<div class="note" id="DYNAMIC_ID">
     <div class="headline">HEADLINE</div>
     <div class="content">Content</div>
</div>

I need something like this:

$(".note").click(function(){
    alert(this+".headline").text();
    alert(this+".content").text();
});

I hope someone can help me with this problem.

+2  A: 

The second parameter of your selector is the context. In this case, we want the context to be the div that had been clicked, so we use the this keyword as the context.

$("div.note").click(function(){
  var headline = $(".headline", this).text();
  alert(headline);
});

You could also use the .find() method too:

var headline = $(this).find(".headline").text();
Jonathan Sampson
HEy thx a lot that is the solution I needed.
Bernhard
You're welcome, Bernhard. You can accept the answer by clicking the check-mark to the left of my response.
Jonathan Sampson
A: 

maybe this could be a solution:

$(".note").click(function(){
alert(".headline").html();
alert(".content").html();

});

or

$(".note").click(function(){
alert("#DYNAMIC_ID").html();

});

Mike
This is all wrong. `Alert` doesn't have an .html() method. I think you meant to have that attached to the selector, of which most of your syntax is missing. The selector doesn't go directly inside the alert command - you're missing the jQuery reference, and there's no context to which .headline you're desiring.
Jonathan Sampson