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 8027903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:57:32+00:00 2026-06-04T23:57:32+00:00

jQuery.get(ajax.php, function(data) { $(data).find(div[id^=’feed’]).each(function() //Find every div with id starting with feed in AJAX

  • 0
    jQuery.get("ajax.php", function(data)
    {
        $(data).find("div[id^='feed']").each(function() //Find every div with id starting with feed in AJAX response
            {
                $('#' + $(this).attr("id")).remove();
            });
        $("#newsfeed").prepend(data);
    });

This works for some divs that fit this description in the ajax response, but not all. Thanks in advance.

Just to make it clear, I want to send out an AJAX request for HTML, itterate through all divs in the response. If they share an id with something already in the DOM AND the id starts with ‘feed’, remove them. Then append the AJAX to the top of newsfeed. Sorry if I didn’t properly explain that before.

  • 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-06-04T23:57:35+00:00Added an answer on June 4, 2026 at 11:57 pm

    I think the problem is with this part:

    $(data).find("div[id^='feed']")
    

    When you pass a string of html to jQuery, like you do with $(data), jQuery creates DOM elements to match the string (these elements are initially detached from the document) and then returns a jQuery object containing only the top-level elements. That is, if your string contained nested elements all of the elements get created but only the top-level ones are returned in the jQuery object.

    .find("div[id^='feed']") then looks for div elements that are descendants of the elements in the jQuery object created in the previous step, so it will not match any top-level elements.

    The .filter() method lets you filter the top-level elements, so you could do something like this:

    jQuery.get("ajax.php", function(data)
    {
       var $newEls = $(data);
    
       $newEls.filter("div[id^='feed']")
              .add( $newEls.find("div[id^='feed']") )
              .each(function() {
           $('#' this.id).remove();
       });
    
       $("#newsfeed").prepend(data);
    });
    

    Within the .each() you can say this.id rather than $(this).attr("id").

    Some example html may help clarify what I tried to explain above. Given:

    data = "<div id='feed1'><div id='feed2'/><div id='xyz'/></div>
            <div id='abc'><div id='feed3'/></div>";
    

    (ignoring the line-break which is there only for readability)

    Then $(data) will create all five divs but return a jQuery object containing only the ‘feed1’ and ‘abc’ divs because they are the only top-level ones.

    $(data).find("div[id^='feed']") returns a jQuery object containing only the ‘feed2’ and ‘feed3’ divs, because they are the only descendants of the elements in the $(data) jQuery object.

    $(data).filter("div[id^='feed']") returns a jQuery object containing only the ‘feed1’ div.

    EDIT: you could probably simplify it with something like this:

    jQuery.get("ajax.php", function(data)
    {
       $("<div/>").append(data).find("div[id^='feed']").each(function() {
           $('#' this.id).remove();
       })
       $("#newsfeed").prepend(data);
    });
    

    This creates a completely empty div as a container so that you can use .find() within that container and do away with .filter().

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

Sidebar

Related Questions

jQuery.get(ajax.php, function(data) { prev = $(#newsfeed).html(); //Remember what was in the newsfeed $(data).find(div[id^='feed']).each(function() //Find
I'm making a AJAX request with jquery like: $.get('/Stuff.php', function (data) { $('#own').html(data); });
I'm loading quite a bit of json data through a jquery/ajax/php get. When I
I have been trying to get a jquery ajax function to return data from
I have a jQuery AJAX call with type:'GET' like this: $.ajax({type:'GET',url:'/createUser',data:userId=12345&userName=test, success:function(data){ alert('successful'); }
My jQuery code (using ajax) request's data from a local php script (pgiproxy.php). This
I have this snippet of code (AJAX) in jQuery. $.get('someScript.php?lat=' + clickedLatLng.lat() + '&lon='
I have use Ajax and jquery for get data from database and send data,
I'm trying to get an jquery ajax callback function to update the background colour
Hello this is code snippet which i get from Jquery Ajax based search I

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.