Super Hornet
Super Hornet

Reputation: 2907

How to get specific element out of returned data

I'm trying to get a specific element out of returned data. here is my HTML:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="js/jquery-1.10.2.min.js"></script>
    <script>
        $(document).ready(function(){
            $.get("data.html",function(data){
                console.log($(data).filter("#app span").html());
            })
        })
    </script>

<body>
<div id="container">
    <div class="a">AAAAAA</div>
    <div class="b">BBBBBB</div>
</div>
</body>
</html>

And data.html:

<div id="app">
    <span>Message</span>
    <div>vjfjdjdj</div>
</div>

I need to get Message inside the <span>, but it returns nothing. Any idea?

Upvotes: 0

Views: 57

Answers (2)

Gabriel Santos
Gabriel Santos

Reputation: 4974

The code below should return in your console all span data

$(document).ready(function(){
    $.get("data.html",function(data) {
        var content= $('<div />').html(data);

        console.log(content.find('#app span').html());
    })
})

Upvotes: 0

Ram
Ram

Reputation: 144689

According to the posted markup the top level element is the #app element, filter method doesn't filter the descendant elements of the top level elements, you should use .find() or .children() method:

console.log($(data).find("span").html());

Upvotes: 1

Related Questions