2

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?

1 Answer 1

1

You need to traverse to paragraph p element in td, then get its text.

Use

$('.block02 h1').text($('#td_1.td_class > p').text());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.