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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:02:59+00:00 2026-05-28T04:02:59+00:00

I saw this: $(div).children().andSelf().contents().each(function(){ if (this.nodeType == 3) { var $this = $(this); $this.replaceWith($this.text().replace(/(\w)/g,

  • 0

I saw this:

$("div").children().andSelf().contents().each(function(){
    if (this.nodeType == 3) {
        var $this = $(this);
        $this.replaceWith($this.text().replace(/(\w)/g, "<span>$&</span>"));
    }
});

here:
wrap each char in except tags with jQuery

Now i try to give each span a id, but they end up all having the same id.
The index that get’s logged to the console however are different.

$("#content").children().andSelf().contents().each(function(index){
    console.log(index);
        if (this.nodeType == 3) {
            var $this = $(this);
            $this.replaceWith($this.text().replace(/(\w)/g, "<span id="+index+">$&</span>"));
        }
    });
  • 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-28T04:02:59+00:00Added an answer on May 28, 2026 at 4:02 am

    This answer might be of use to you.

    If you want each letter in a span, an an unique id for each one, here’s how it could be done:

    $("#content").children().andSelf().contents().each(function(index){
        if (this.nodeType == 3) {
            var $this = $(this);
            $this.replaceWith($this.text().replace(/\w/g, function(text,index2) {
                return "<span id="+index+""+index2+">" + text + "</span>";
            }));
        }
    });
    

    Obs.: I’m not sure every browser support this form of Regex.replace. Tested with FF3.6 and Chrome.

    Clarifying a bit: This form is new to me too, just learned today, but couldn’t find relevant documentation online. It appears that, for a regex without capturing groups, the first argument is the matched text and the second it’s index in the original string. Otherwise, the second argument becomes the first captured group, and I assume the 3rd is the 2nd c.g. and so on… If anyone know where to find more info, I’m interested in it too.

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

Sidebar

Related Questions

<div onclick=alert(this.innerHTML);> <p>Since you saw <span title=:P class=emoticon tongue></span> if <strong>you read</strong> the intro
I saw this in some code: $(<p/>).append(<div>something</div>).appendTo(body); and noticed that it automatically closed the
I just saw this code in an answer: HTML <div class=thumbnail> <img src=http://placehold.it/50x50> <img
I saw this in jQuery tools in the tooltip script. What is <div/> ?
This is my jquery: $(document).ready(function(){ $(div#overlay div#getFBid div#overlayClose).on(click, function(){ console.log(test) }); }); And this
I saw this as an example on MDN and didn't understand why this was
I saw this C# using statement in a code example: using StringFormat=System.Drawing.StringFormat; What's that
I saw this question and it motivated me to look again (without success) at
I saw this posting which explained how to get BC3 working as the diff
i saw this In MySQL, joins work for INSERT, UPDATE, and DELETE statements. It's

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.