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

  • SEARCH
  • Home
  • 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 845633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:29:01+00:00 2026-05-15T06:29:01+00:00

There are many class=item blocks on the page. For each one there is different

  • 0

There are many class="item" blocks on the page.

For each one there is different var item_link and ajax request.

Ajax searches for src attribute of .message img and throws it to var src.

$(".item").each(function(){
    var item_link = "http://...";
    $(this).prepend('<div class="src"></div>');
    $.get(item_link, function(data) {
        var src = $('.message img', data).attr('src');
    });
});

How to print var src to <div class="src"></div>?

Thanks.

  • 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-15T06:29:01+00:00Added an answer on May 15, 2026 at 6:29 am

    jAndy’s approach should work, but it waits to add the div until the GET completes (which should be fine). If it’s really important that you put the div in place before doing the GET, though, you could do this:

    $(".item").each(function(){
        var item_link = "http://...";
        var item_div = $('<div class="src"></div>');
        $(this).prepend(item_div);
        $.get(item_link, function(data) {
            var src = $('.message img', data).attr('src');
            item_div.text(src);
        });
    });
    

    That uses item_div.text(), which will show the value from src. If src has HTML tags and you want them to be rendered, use item_div.html() instead.

    Edit: Update after your “doesn’t work” comment:

    The part you asked about, setting the text of the div, works fine. I think the problem is that your code is assuming that the data coming back from the ajax call will be a DOM element. It won’t, it’ll be a string until you insert it into the DOM somewhere (jQuery doesn’t turn HTML into DOM objects proactively).

    This version handles that:

    $(".item").each(function(){
        var item_link = "http://...";
        var item_div = $('<div class="src"></div>');
        $(this).prepend(item_div);
        $.get(item_link, function(data) {
            var img = $(data).find('.message img'); // <== Make a DOM element, 
                                                    //     look for images in
                                                    //     .message containers
            var src = img.attr('src');
            item_div.text(src);
        });
    });
    

    Working example:

    <!DOCTYPE HTML>
    <html>
    <head>
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
    <title>Test Page</title>
    <style type='text/css'>
    body {
        font-family: sans-serif;
    }
    </style>
    <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js'></script>
    <script type='text/javascript'>
    (function() {
        $(document).ready(pageInit);
    
        function pageInit() {
            $('#btnGo').click(go);
        }
    
        function go() {
    
            $(".item").each(function(index){
                var item_link = "tempserver.jsp?index=" + index;
                var item_div = $('<div class="src"></div>');
                $(this).prepend(item_div);
                $.get(item_link, function(data) {
                    var img = $(data).find('.message img');
                    var src = img.attr('src');
                    item_div.text(src);
                });
            });
    
        }
    })();
    </script>
    </head>
    <body>
    <input type='button' id='btnGo' value='Go'>
    <div>
        <div class='item'>Item 1</div>
        <div class='item'>Item 2</div>
        <div class='item'>Item 3</div>
        <div class='item'>Item 4</div>
    </div>
    </body>
    </html>
    

    tempserver.jsp:

    <div>
    <div class='message'><img src='image<%=request.getParameter("index")%>.png'></div>
    </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are many methods for representing structure of a program (like UML class diagrams
There are many classes having this provider suffix. (Data,membership,modelmetadata,...). When should be a class
I found that there are many frameworks that will check the duplicate class name
How many interfaces can a class file implement? Is there a limit on the
I'm using a many-to-one relationship as follows: Classes: public class Account { public virtual
I need to implement input validation throughout my winform app. There are many different
I have an unordered list and each of the item in the ul has
There is a table Item like, code,name 01,parent1 02,parent2 0101,child11 0102,child12 0201,child21 0202,child22 Create
I have list item with EditText in it, I don't know how many items
Possible Duplicate: C# virtual static method Idea: public class Item { public Vector2 Position;

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.