Reputation: 369
I have the following code:
Array part:
var menu =
[
{
td: {'id': 'td_1'},
a: {'class': 'td_class', 'id': 'td_id_1'},
p: {'title': 'TD1', 'style': 'color:#fff;'}
},
{
td: {'id': 'td_2'},
a: {'class': 'td_class_2', 'id': 'td_id_2'},
p: {'title': 'TD2', 'style': 'color:#fff;'}
}
];
and rest of the js code:
$.each(menu, function(i, item){
$('<td>').attr('id', item.td.id)
.append($('<a>', {'class': item.a.class, 'id': item.a.id})
.append($('<p>', {'text': item.p.title, 'style': item.p.style})))
.insertBefore('#menu');
});
HTML part:
<div class="block02">
<h1 id="h1" style="color:#fff;"></h1>
</div>
<td id="menu"></td>
I want to add text from array and insert into h1:
if ($('#td_1').hasClass('td_class')){
$('.block02 h1').text(td.p.title);
}
} else ....
Of course I get nothing. My question is: How to get "TD1" as h1's text value?
Upvotes: 2
Views: 145
Reputation: 133403
You need to traverse to paragraph p
element in td
, then get its text.
Use
$('.block02 h1').text($('#td_1.td_class > p').text());
Upvotes: 1