weinraum
weinraum

Reputation: 11

how to address a child element in a div, addressed by its index

To address an element by its index is clear:

inner= $("span.[class*="myClass"]")[indTextTitle].innerHTML;

now, I have some div containing a I'ld like ta adress

<div class='row'><a class'title'> ... </a>...<p class='this'></p></div>
<div class='row'><a class'text'> ... </a>...<p class='that'></p></div>
<div class='row'><a class'title'> ... </a>...<p class='more'></p></div>

a function is clalled when clicked a link

$(function() { $("a.title").click(function(){
var indTitle = $("a.title").index(this); 
var Index =   $($("a.title")[indTitle]).parents("div.row").index();

What must be stupidly simple is to adress one of the p - elements by the index of the div:

inner = S("div.row[index] p").innerHTML.

However, I'm more stupid. I've checked lots of ways, nothing worked once the brackets of the index appear. Again, no problem to adress the p.this[xy].innerHTML directly. But its index is changed dynamically and the only logical access is the index of the div.

Thanks for any help

Thomas

Upvotes: 1

Views: 409

Answers (2)

pete
pete

Reputation: 25091

By index, try:

inner = $($("div.row")[Index]).find('p').html();

Alternatively, since the p element you are interested in appears to always be the last child of div.row, you could try:

inner = $(this).parent().find('p:last').html();

assuming that inner is defined in the click handler for the a.

Upvotes: 1

Stefan
Stefan

Reputation: 5662

You can use the eq() function or the :eq-selector as $('a.title:eq(1)') or $('a.title').eq(1).

Upvotes: 3

Related Questions