0

I am trying to "encapsulate" a web into a phonegap app, but I am finding a problem. When I make a request to the server it answer me with the html as a text, and I want to take some html elements of this text and append on a div of the index.html of the application. To make this I do so:

jQuery.get(rute, function(htmlTemplate) 
{
    var data = jQuery(htmlTemplate).find('#wrapper');
    //make a small treatment to data, but not yet.
    jQuery('body').html(data)
});

I have other application where I do something similar:

function loadPage(placeToLoad, rute, callback) 
{
    jQuery.get(rute, function(htmlTemplate) 
    {
        var data = jQuery(htmlTemplate).find('#wrapperApp').parent(); 
        if(data.html()!==undefined)
        {
            placeToLoad.html(data);
            data = eraseNoScriptExternalPages(data);
            data = eraseStylesExternalPages(data);
            data = eraseLinksExternalPages(data);
            makeImageScroll(data);
        }
        else
            placeToLoad.html(htmlTemplate);     
        if (callback!=undefined && typeof(callback)==typeof(Function)) 
            callback();
    });
}

In this second case I need to differentiate between the pages that came from a external server (which must be engineered to not cause collateral damage), and local pages.

The case is that the second case works and the first not!!! why!!??

I was trying to make a simple test:

var html = '<head><style> body{background-color:rgba(128,128,128,0.3);}</style> </head> <body> <div id="test">HOLA</div> </body>';
jQuery("#content").html(jQuery(html).find("#test").html());

...but doesn´t work.

I want to choose a specific element to make the small treatment (erase any divs or an aside section).

2 Answers 2

1

jQuery(a).find(b) will only find b that is a descendent of a. In your example, #test is the root node, so it's both a and b, so there will be no matches.

Instead, you can use .filter():

var html = '<head><style> body{background-color:rgba(128,128,128,0.3);}</style> </head> <body> <div id="test">HOLA</div> </body>';
jQuery("#content").html(jQuery(html).filter("#test").html());

http://jsfiddle.net/gg5r6mqp/

Sign up to request clarification or add additional context in comments.

Comments

1

Hopefully this can helps for you. Here I have created temporary DOM object and querying against that:

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            var html = '<head><style> body{background-color:rgba(128,128,128,0.3);}</style> </head> <body> <div id="test">HOLA</div> </body>';
            var mydiv = document.createElement('div');
            mydiv.innerHTML = html;

            alert(jQuery($(mydiv)).find("#test").html());
        });

    </script>
</head>
<body>
</body>
</html>

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.