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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:27:00+00:00 2026-05-25T01:27:00+00:00

I have some html element like these: <table id=myTable></table> <select name=mySelect> <option value=1>1</option> <option

  • 0

I have some html element like these:

 <table id="myTable"></table>

    <select name="mySelect">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    </select>

    <a href="javascript:void(0)" onclick="addToTable()">Add new</a>

    <script>
    addToTable = function() {
      var selected = $("select[name*='mySelect'] option:selected").val();
      $('#myTable').find('tr').each(function() {
        if ($(this).attr('id')==selected) { 
          alert('Record has already existed!'); return false;
        }
        else $('#myTable').append('<tr id="'+selected+'"><td>'+selected+'</td></tr>');
      });
    }
    </script>

The problem was: when I added two records (rows) with the same id, it alerted the message but kept appending the new row instead of breaking out the loop. What was I wrong here?

Thanks in advance.

  • 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-25T01:27:00+00:00Added an answer on May 25, 2026 at 1:27 am

    What you are doing:

    for every existing row R
      if R.id == newRow.id
        alert
        break
      else
        add newRow
    

    This will add the newRow for each row that comes before an existing row with that id. With rows [1,2,3,4,5,6,7,8,9] and adding a row 9 will add that row 8 times before alerting “already exists”.

    What you mean to do is:

    exists = false
    for every existing row R
      if R.id == newRow.id
        existing = true
        alert
        break
    
    if !exists
      add newRow
    

    Equivalent in JS:

    addToTable = function() {
      var selected = $("select[name*='mySelect'] option:selected").val();
      var exists = false;
      $('#myTable').find('tr').each(function() {
        if ($(this).attr('id')==selected) { 
          alert('Record has already existed!');
          exists = true;
          return false;
        }
      });
      if(!exists) {
        $('#favourite_hotels_table').append('<tr id="'+selected+'"><td>'+selected+'</td></tr>');
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a via with some jQuery function to get value from HTML element
I have a JavaScript library that updates a hidden <textarea> HTML element with some
I have HTML page with some HTML element with ID=logo . I need to
I have some HTML that looks like this: <ul class=faq> <li class=open> <a class=question
I have a class that generates some html (form elements and table elements), but
I have some HTML like this: <a class=button> <span> <span> <span class=buttonspan>Content</span> </span> </span>
I have a simple form with some plain html input like bellow using ASP.NET
I have a table in HTML with a few rows. I originally gave some
I have some existing code. This code uses an html table for a banner.
I have an HTML table, to which I would like to add or remove

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.