0

I have the following html string:

<html>
<head>
</head>
<body>
    <table>
        <tbody>
            <th>
              <tr>
                <img src=""/>
              </tr>
            </th>
        </tbody>
    </table>
</body>
</html>

Is there a way to get only the img element?

So in this case I want a string like this <img src=""/>.

More specific example:

$wikiDOM = $("<document>"+ wikiHTML+ "</document>");
var infobox = $wikiDOM.find('.infobox').html();
if(infobox != 'undefined' && infobox !== ""){
    var img = infobox.filter( 'img' );
    console.log( img[0].outerHTML );
}

where wikiHTML is the request i make to wikipedia api in json format for several searchterms.

Edit:

return from img and infobox:

image:[object Object] 
infobox[object Object] 
image:[object Object] 
infobox[object Object]
image:[object Object] 
infobox[object Object]
image:[object Object] 
infobox[object Object] 
image:[object Object] 
infobox[object Object]
image:[object Object] 
infobox[object Object]
image:[object Object] 
infobox[object Object] 
image:[object Object] 
infobox[object Object] 
2
  • Do you actually have a string? or it that the source code? Commented Jul 29, 2014 at 14:40
  • I actually have a string Commented Jul 29, 2014 at 14:47

2 Answers 2

6

Yes, for example in jQuery you will need to pass your string to $ function and then get <img> by img selector:

var html = $( '<html><head></head><body><table><tbody><th><tr><img src=""/></tr></th></tbody></table></body></html>' )
var img = html.filter( 'img' );
console.log( img );

To get img html code you will need:

console.log( img[0].outerHTML )

EDIT So according to your edit your code should be:

$wikiDOM = $("<document>"+ wikiHTML+ "</document>");
var infobox = $( $wikiDOM.find('.infobox').html() );
if(infobox != 'undefined' && infobox !== ""){
    var img = infobox.filter( 'img' );
    console.log( img[0].outerHTML );
}

As you can see I have added $() around $wikiDOM.find('.infobox').html(). You were getting undefined because innerHTML DOM property doesn't have filter method. Only jQuery do.

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

9 Comments

i am getting undefined in html.find('img');
@João ok, use filter instead of find method. See example: jsfiddle.net/ex8Hk
In my case i have a var like this. var infobox = $wikiDOM.find('.infobox').html(); to get html of infobox from wikipedia. with filter i still get undefined.
This is off-topic. But if you want to parse wikipedia, you might consider using their JSON API mediawiki.org/wiki/API:Data_formats
i am getting json wikipedia infobox for several searchterms but it is not relevant. because i than have the html inside the infobox. i added code to question.
|
0

This would do:

var image = $("img").html();

1 Comment

This will return nothing as html() method return's child nodes of current selector and works like innerHTML, you need outerHTML here

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.