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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:52:47+00:00 2026-05-15T15:52:47+00:00

How do I use jQuery .each() on a string // For Exmaple var mystring

  • 0

How do I use jQuery .each() on a string


// For Exmaple

var mystring = '<div> bleh content </div> <div> bleh content </div>';

$('div', mystring).each(function(e) {
  alert('do something');
});

// the above code isnt launching an alert for each div in the string? Im not sure why?

  • 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-15T15:52:48+00:00Added an answer on May 15, 2026 at 3:52 pm

    The way you’re doing it, you’re searching for div elements inside the elements passed in. Basically equivalent of doing a .find().

    What you want is filter() which will filter over the top level elements in the collection you passed.

    Test it here: http://jsfiddle.net/u5uDg/

    var mystring = '<div> bleh content </div> <div> bleh content </div>';
    
    $(mystring).filter('div').each(function(e) {
        alert('do something');
    });
    

    If you wanted to use your approach, you would need to give the div elements a parent on which jQuery can perform the find.

    http://jsfiddle.net/u5uDg/1/

          // Added parent <div> element
    var mystring = '<div><div> bleh content </div> <div> bleh content </div></div>';
    
    $('div', mystring).each(function(e) {
      alert('do something');
    });
    

    As requested in your comment, you can delay execution of the code in the .each() using setTimeout() and arriving at the duration of each by multiplying the current iteration number by the number of milliseconds you want to delay.

    http://jsfiddle.net/u5uDg/6/

    var mystring = '<div> bleh content </div> <div> bleh content </div>';
    
       // Get the length
    var length = $(mystring).filter('div').length;
    
    $(mystring).filter('div').each(function(e) {
        // setTimeout is used to delay code from executing.
        // Here we multiply e (which is the index of the current 
        //   iteration) by 2000 milliseconds, so each iteration
        //   is delayed by an additional 2000ms
        (function(th) {
            setTimeout(function() {
                     alert($(th).text());
                     if(!--length) { alert('done'); } // alert if done
            }, e * 2000);
        }(this));
    });​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use the jQuery each function (line 5) to display the
I'm trying to use the .each() function of JQuery on a body load to
I used to use this script for jquery email obfuscation: $(.replaceAt).replaceWith(@); $(.obfuscate).each(function () {
I use jquery post function to go to server and bring the link to
I have a string of HTML that I use as jQuery input document. //
I want to use jQuery function to fetch web service response but getting error
Is there a way to use jQuery to search a <p> and do something
I asked at Parsing HTML String with jQuery how I can use jQuery on
jQuery allows use of the string values slow, normal, and fast in animations (e.g.:
Use jquery + php. Doing custom AJAX captcha. i.e user clicks on image, it

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.