Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7547673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:30:45+00:00 2026-05-30T09:30:45+00:00

I have an html string I’d like to dump directly into an element. This

  • 0

I have an html string I’d like to dump directly into an element. This html string contains some tags, and I need to prepend a path to their ‘src’ attributes.

Currently, I’m doing something like this:

// note, htmlString is coming in from an external xml file...
var htmlString = "<img src='img1.jpg'/><br/><img src='img2.jpg'/>";
var imgContainer = $('<div />');
imgContainer.append(htmlString);

var prefix = "some/path/to/img/";
imgContainer.find('img').each(function(i, el) {
    $(el).attr('src', prefix + $(el).attr('src'));
});

This works, but I’m seeing failed resource loads of the non-prepended path. Seems as though the browser is attempting to load the images immediately upon creation of the imgContainer element, even though it’s not appended to the document. I’d like to avoid those failed loads.

I suppose I could parse htmlString before appending but it’s nice to just $.each() through the element after it’s created…

NOTE: I’ve learned through asking this question that creating a jQuery element via $(htmlString) immediately causes load attempts on all resources within htmlString. So, I have to manipulate htmlString in some way before packing it into a jQuery object.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:30:46+00:00Added an answer on May 30, 2026 at 9:30 am

    You can try to parse the htmlString as XML, modify it, and then append it to the DOM.

    Something like this (Not sure if XMLSerializer works in all browsers, I think it’s this.xml in IE):

    var htmlString = "<img src='img1.jpg'/><br/><img src='img2.jpg'/>",
    imgContainer = $('<div />')
    xml = $.parseXML('<xml>'+htmlString+'</xml>'), // add `<xml>` because
                                                   // XML needs a root element
    prefix = "some/path/to/img/",
    // needed to convert XML to HTML
    sXML = typeof XMLSerializer !== 'undefined' && new XMLSerializer;
    
    $(xml).find('img').each(function(i, el) { // replace the SRC
        $(el).attr('src', prefix + $(el).attr('src'));
    });
    
    // Convert XML to HTML
    // http://stackoverflow.com/a/829724
    htmlString = $(xml).find('xml').contents().map(function(i,v){
        return v.xml || sXML.serializeToString(v);
    }).get().join('');
    
    imgContainer.append(htmlString);
    

    DEMO: http://jsfiddle.net/de2dg/3/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a HTML string like this: <td class=name> <a href=/blah/somename23123>Some Name</a> </td>
I have a HTML string ( not DOM element ) like : <p>This is
I have an HTML string representing an element: '<li>text</li>' . I'd like to append
I have a string containing HTML and I need to replace some words to
I have a HTML string in ISO-8859-1 encoding. I need to pass this string
I have a html string like this: <html><body><p>foo <a href='http://www.example.com'>bar</a> baz</p></body></html> I wish to
I have an attribute in my model which contains html string like below: <p>Solution
I have an HTML string containing multiple <image> tags. I'd like to search for
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
So, I have an HTML string that looks just like this: <div> <div class=venue-tooltip>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.