Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

139
Views
Making jquery object append faster

I am using this code to hide and append a custom input after all ".word" elements, which is extremely slow. I suspect this is because it updates the DOM and reflows the document every time.

Is there any way to speed up this? Problem is, as you can see, that each input box is kind of customized with a particular maxlength, lineheight, etc. so it seems to me it is not possible to kind of do the processing before and then append everything to the DOM at the end. Maybe cloning could work, but I don't know how to implement it.

var $elems = $(".word");
$elems.each(function(index, value) {
    var $elem = $(this);
    var length = $elem.text().length;
    var width = $elem.width();
    var line_height = $elem.css("font-size");
    $elem
        .hide()
        .after(
            '<div class="input-group dict-input-group"><input type="text" class="dict" ' +
                'style="width:' +
                width +
                "px; line-height:" +
                line_height +
                ';" ' +
                'maxlength="' +
                length +
                '" data-text="' +
                $elem.text() +
                '">' +
                '<span class="input-group-append dict-answer d-none"></span></input></div>'
        );
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I solved it with by cloning the container, doing the processing in the cloned element and then replacing the original element:

var $container = $("#text").clone();
var $elems = $container.find(".word");
var $original_elems = $(".word");

$elems.each(function(index, value) {
    var $elem = $(this);
    var length = $elem.text().length;
    var width = $original_elems.eq(index).width();
    var line_height = $original_elems.eq(index).css("font-size");
    $elem
        .hide()
        .after(
            '<div class="input-group dict-input-group"><input type="text" class="dict" ' +
            'style="width:' +
            width +
            "px; line-height:" +
            line_height +
            ';" ' +
            'maxlength="' +
            length +
            '" data-text="' +
            $elem.text() +
            '">' +
            '<span class="input-group-append dict-answer d-none"></span></div>'
        );
});

$("#text").replaceWith($container);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!