60

I have an HTML string from a Ajax loaded source. I would like to get some attributes from an object (image) in this string, before I put the HTML into the document.

I've got something like:

$.ajax({  
        url: uri+'?js',  
        success: function(data) { 
            var htmlCode = $(data).html();  

            $('#otherObject').html(data);
        }  
    });

How can I get attributes (the src for example) from this HTML string?

4 Answers 4

107

I'm not a 100% sure, but won't

$(data)

produce a jquery object with a DOM for that data, not connected anywhere? Or if it's already parsed as a DOM, you could just go $("#myImg", data), or whatever selector suits your needs.

EDIT
Rereading your question it appears your 'data' is already a DOM, which means you could just go (assuming there's only an img in your DOM, otherwise you'll need a more precise selector)

$("img", data).attr ("src")

if you want to access the src-attribute. If your data is just text, it would probably work to do

$("img", $(data)).attr ("src")
Sign up to request clarification or add additional context in comments.

7 Comments

uuh, thanks for your answer! it's the solution :) i didn't see it. i wish you a good first april - and now i'll get in shame ;)
mhm, i've to regreat my comment. your example doens't work for ie (tested in version 7). i used "$(data)" - "data" - "$(data).html()" - nothing works. any suggestions?
$("img", $('<div><img src="example.com/test.jpg" /></div>')) worked in FF (haven't got IE7). Having just the IMG tag enclosed, or enclosed in just a body did NOT work, so perhaps it translates to a single entity? Did you try $(data).attr () directly?
i tried $(data).attr() but this doesn't work either. i think it doesn't work in general with ie 7. i've to look for a workaround for exmaple to put the code into a hidden div, read the attribute and then show the code official in the document.
@Roe really thanks for your code... It was about 2 hours working on this and now i found the way (your code)... thanks again
|
2

MarvinS.-

Try:

$.ajax({  
        url: uri+'?js',  
        success: function(data) {  
                var imgAttr = $("img", data).attr('src'); 
                var htmlCode = $(data).html();
                $('#imgSrc').html(imgAttr);
                $('#fullHtmlOutput').html(htmlCode);
        }  
    });

This should load the whole html block from data into #fullHtmlOutput and the src of the image into #imgSrc.

Comments

1

just add container element befor your img element just to be sure that your intersted element not the first one, tested in ie,ff

Comments

1

One thing to note - as I had exactly this problem today, depending on your HTML jQuery may or may not parse it that well. jQuery wouldn't parse my HTML into a correct DOM - on smaller XML compliant files it worked fine, but the HTML I had (that would render in a page) wouldn't parse when passed back to an Ajax callback.

In the end I simply searched manually in the string for the tag I wanted, not ideal but did work.

1 Comment

This could help if you are parsing whole page: stackoverflow.com/a/1069953/854477

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.