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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:07:11+00:00 2026-06-18T21:07:11+00:00

I have an html table which is hundreds of rows long that contains a

  • 0

I have an html table which is hundreds of rows long that contains a list of strings. I also have an input box that will insert new rows into the table. The second td element in each row is always a string within a div, so a row in the table might look like this:

<tr>
    <td><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>
    </td>
    <td class="a_string" style="width:200px;">
        <div style="word-wrap:break-word;">hello world</div>
    </td>
    ...
</tr>

I want to insert new rows into this table based on a list of strings from the user, if a string is already in the table it will be ignored and not inserted. The problem arises when the list of strings from the user is very long and the number of rows in the table is very long, the hastily written method i am using to check for duplicates is much too inefficient for this task:

function check_rows(item) { //called on each item in input list
    var tf = false;
    $('#mytable tbody').children().each(function (i) {
        //loop through each tr

        if ($('#mytable tbody').children().eq(i).children().eq(1).hasClass('a_string')) {
            //if the row is a string to compare (second td element has the class 'a_string')

            if (item == $('#mytable tbody').children().eq(i).children().eq(1).children().eq(0).html()) {
                //find the string within the div within second td in row "i" (current indexed row)
                //compare the strings and if it is found in the table break out of the loop

                tf = true;
                return false;
            }
        }
    });
    //if the item is not found in any of the rows false will be returned 
    return tf;
}

The function does what it is supposed to do (check for duplicate strings), it is just way too slow. I tried to insert the list without this function and it was almost instantaneous, so the problem is here.

Any suggestions on how to to improve this function would be greatly appreciated!

  • 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-18T21:07:12+00:00Added an answer on June 18, 2026 at 9:07 pm

    You can try below code. You don’t have to use $('#mytable tbody') inside each. Instead use this.

    Try to assign elements to a variable and it will create a reference which will be much faster.

    function check_rows(item) { //called on each item in input list
        var tf = false;
        $('#mytable tbody').children().each(function (i) {
            //loop through each tr
    
            var tr = $(this).eq(i).children();    
            if (tr.eq(1).hasClass('a_string')) {
                //if the row is a string to compare (second td element has the class 'a_string')
    
                if (item == tr.eq(1).eq(0).html()) {
                    //find the string within the div within second td in row "i" (current indexed row)
                    //compare the strings and if it is found in the table break out of the loop
    
                    tf = true;
                    return false;
                }
            }
        });
        //if the item is not found in any of the rows false will be returned 
        return tf;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a basic HTML table which contains table rows. My goal is to
I have a Html Table which consists of rows that were generated dynamically (from
I have an HTML table which may contain thousands of rows (number of columns
I have an HTML table which has equally divided rows and columns. I would
I have a HTML table which I want to make fluid so that when
I have an html table which contains records, comes from mysql db. Each row
I have a html table which consists of columns for days, and rows for
I have a HTML table that contains price information. The table is sorted be
I have a static Html Table which contains one button Add and when clicking
I have a html table which has several rows - lets say 17 for

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.