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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:20:54+00:00 2026-06-07T22:20:54+00:00

I have this HTML structure: <p id=lorem> <span class=part id=n01>L</span><span class=part id=n02>o</span><span class=part id=n03>r</span><span

  • 0

I have this HTML structure:

    <p id="lorem">
      <span class="part" id="n01">L</span><span class="part" id="n02">o</span><span class="part" id="n03">r</span><span class="part" id="n04">e</span><span class="part" id="n05">m</span> <span class="part" id="n06">i</span><span class="part" id="n07">p</span><span class="part" id="n08">s</span><span class="part" id="n09">u</span><span class="part" id="n10">m</span>
    </p>

​<h1>Click Me</h1>​​​​​​​​​​​​​​​​​

and this JavaScript code:

​arrRed = ["#n01", "#n04", "#n05", "#n09"];​​​​​​​​​
arrBlue = ["#n02", "#n07", "#n08"];

$("h1").click(function(){

    $.each(arrRed, function( i, v ){
        $(v).addClass("red");
    });

    $.each(arrBlue, function( i, v ){
        $(v).addClass("blue");
    });
})

http://jsfiddle.net/FF2Dr/

When the h1 is clicked, certain letters get a certain color. The problem with that is the performance, I trigger too much reflows/repaints.
​
In another question someone told me to use parent.cloneNode(true) and parent.parentNode.replaceChild(clone, parent). So I clone the elements, change them and but them back in place, triggering only one reflow.

So I cloned the parent element:

var doc = document;
var lorem = doc.getElementById("lorem");
var clone = lorem.cloneNode(true)

But how do I have to proceed now? I somehow have to modify the elements in clone and then replace them, but using $.each again to edit them does not seem to be a good idea.

http://jsfiddle.net/FF2Dr/1/

  • 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-07T22:20:56+00:00Added an answer on June 7, 2026 at 10:20 pm

    Try this:

    $("h1").click(function(){
        var lorem = $("#lorem"),
            clone = lorem.clone(true);
        clone.children().filter(function() {
            return $.inArray('#' + this.id, arrRed) != -1;
        }).addClass("red").end().filter(function() {
            return $.inArray('#' + this.id, arrBlue) != -1;
        }).addClass("blue");
        lorem.replaceWith(clone);
    });
    

    Fiddle

    Create a clone, filter the children of this new cloned element twice to apply the classes and finally replace the old element entirely with the new one.

    Or, if you prefer, using find instead of filter while iterating over the arrays:

    $("h1").click(function(){
        var lorem = $("#lorem"),
            clone = lorem.clone(true);
        $.each(arrRed, function(i, v) {
            clone.find(v).addClass("red");
        });
        $.each(arrBlue, function(i, v) {
            clone.find(v).addClass("blue");
        });
        lorem.replaceWith(clone);
    });
    

    Fiddle

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

Sidebar

Related Questions

So i have this structure html <span class=label data=data/support_icon.png> <span class=title>1 User/Location - 8Am
Hello I have this html structure. <span class=radio style=background-position: 0px 0px; ></span> I just
I have this HTML structure <div class=buttons> <button data-icon=ui-icon-disk class=save>Save1</button> <button data-icon=ui-icon-check class=ok>OK1</button> <button
I have this HTML structure (excerpt): <td> <select id=test1 class=pub_chooser> <option value=99>All Publications</option> <option
I have this html structure <div class=top_menu> <ul> <li class=left><a href= class=family-filter>asdas</a></li> <li class=welcome>Welcome
Assuming I have this HTML structure: <ul class='menu'> <li> <div></div> <div> <div></div> <div> <a
i have this following html structure usilg ul and li. <ul class=treeview id=productTree> <li
I have this html structure: <div id=home> <div class=section_header> <h1 class=left data-target=newest><?=lang('home_newest');?></h1> ... </div>
I have this HTML structure: <div class=start> <div class=someclass> <div class=catchme> <div=nested> <div class=catchme>
I have a HTML structure like this: <ul id=carousel class=jcarousel-skin-photos> <li><a href=images/content/featuredPhoto.jpg><img src=images/content/featuredPhoto.jpg alt=Lorem

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.