tags:

views:

48

answers:

4

Hello

I want to obtain the .text() of #inner2

<div class="outer" id="outer">

<div id="inner1" class="inner">test1</div>
<div id="inner2" class="inner">test2</div>
<div id="inner3" class="inner">test3</div>


</div>

This is the jquery function I am using

$('.outer').bind('click',function() {


var one = $('#inner'+x).attr('id');
alert(one);


});

The problem is the first #id value is show in the alert.

Thanks

Jean

+2  A: 

Hi there.

$('.outer').bind('click',function() {


var one = $('#inner2').attr('id');
alert(one);
Jason Evans
check the q again, did some changes
Jean
+1  A: 
$('.outer').bind('click',function() {


  var one = $('#inner2').text();
  alert(one);


});
bsboris
check the q again, did some changes
Jean
A: 

If it's just to retrieve the inner2 on click:

$("#outer").click(function() {
  alert($("#inner2").text());
});

But if you actually are trying to get the text of the clicked inner div, then the following code will work:

$("#outer .inner").click(function() {
  alert($(this).text());
});

If it's to retrieve the ID, then just change text() to attr('id').

Gert G
+1  A: 

You can use .each to iterate through the divs with class name inner and then fetch the ids.

$('.outer').bind('click',function() {
    $("div.inner").each(function(){
        alert ($(this).attr("id"));
    });
});

If you want the id of the clicked one then use event.target

like

$('.outer').bind('click',function(e) {
    alert (e.target.id);
    alert($(e.target).text()); 
    // to get text wrap e.target to a jquery object and use .text() on that
});
rahul
I want the clicked ones id, not every #id value
Jean
so what exactly was wrong in my original code logically?
Jean
I want to get the text tooheres my code var ee = (e.target.id); var ee1 = ('#'+ee).text(); alert(ee1);does not work though
Jean
See my edited answer.
rahul