tags:

views:

8159

answers:

3

hello,

i have a html-string from a ajaxloaded source and i would like to get some attributes from an object (image) in this string, before i put the whole html into the document.

since now i've something like:

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

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

i hope you can help me. and i hope that the solution isn't that simple that i'll get in shame.

thanks a lot!

+4  A: 

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")
roe
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 ;)
MarvinS
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?
MarvinS
$("img", $('<div><img src="http://www.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?
roe
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.
MarvinS
A: 

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.

Sean
A: 

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