msqar
msqar

Reputation: 3040

how do i get div childs from jquery/javascript in this example?

i'm trying to access to the div's child by using jquery's .eq() function, but it looks like something's wrong, despite the fact is not throwing an error, looks like when i do a .innerHTML to the desired child div element, nothing happens.

This is my HTML:

<div id="status_container">
    <div class="status_item_wrapper">
        <div class="status_item_title">
            <span>TITLE 1</span>
        </div>
        <div class="status_item_content">
            <table id="box-table"></table>
        </div>
    </div>
    <div class="status_item_wrapper">
        <div class="status_item_title">
            <span>TITLE 2</span>
        </div>
        <div class="status_item_content">
            <table id="box-table"></table>
        </div>
    </div>
</div>

And this is my javascript:

function doSomething(message) {
     var data = message.data;
     var index_container = 0;

     var container = $("#status_container").eq(0);
     var content_wrapper = container.eq(1); // this would be the content div of each child
     content_wrapper.html(JSON.stringify(data));
}

I thought this would get the "TITLE 1" status_item_wrapper div, and then, content_wrapper would contain the "status_item_content" object.

How am i supposed to reach the "status_item_content" div from the very first parent "status_container"?

Thanks.

Upvotes: 1

Views: 121

Answers (1)

adeneo
adeneo

Reputation: 318372

content_wrapper is a jQuery object, as that is what eq() returns, and does'nt have an innerHTML method, you should use jQuery's html() :

content_wrapper.html( JSON.stringify(data) );

to return the native DOM element instead, you can use get(), and do :

var content_wrapper = container.get(1);
content_wrapper.innerHTML = JSON.stringify(data);

EDIT:

Now that you've added container, there are some issues. eq() will get a certain element in a collection of elements based on a zero based index, and you're using an ID as a selector, so there should'nt really be a collection, as ID's are unique, and should only select one single element.

If you're trying to select the second child inside the #status_container element, you'd do

 var content_wrapper = $("#status_container").children().eq(1);

Upvotes: 2

Related Questions