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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:23:41+00:00 2026-05-24T06:23:41+00:00

I want to check if a specific cell in a table contains a specific

  • 0

I want to check if a specific cell in a table contains a specific form using JavaScript.

Here is what I have done:

for(var i = 1; i >= mainTable.tBodies[0].rows[2].length; i++)
{
    if(mainTable.tBodies[0].rows[2].cells[i].elements[0] == document.getElementById("my_form"))
    {
        var cellIndex = i;
        alert("cell" + i + " contains the form");
        break;
    }
}

"my_form" is actually inside cell number 1, but the if-statement is always false despite it should be true at i = 1. How can I achieve this in JavaScript?

EDIT:

Here is the whole code:

<HEAD>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin

//var mainTable = document.getElementById("mainTable");
var index_Skill = 1;
var index_Speed = 1;
var index_Age = 1;
var index_Strength = 1;
var index_Height = 1;

function moveUp(theForm)
{
    if(theForm == "form_Skill")
    {
        if(index_Skill == 1)
        {
            // get the max index of the table
            var newIndex = Math.max(++index_Skill, ++index_Speed, ++index_Age, ++index_Strength, ++index_Height);

            // insert a new row
            var newRow = mainTable.tBodies[0].insertRow(newIndex);

            // shift the rows one level down
            for(var i = newIndex; i > 1; i--)
            {
                mainTable.tBodies[0].moveRow(i - 1, i);
            }

            // delete (skill) from the old index
            var cellIndex;
            for(var i = 1; i >= mainTable.tBodies[0].rows[2].length; i++)
            {
                if(mainTable.tBodies[0].rows[2].cells[i].elements[0] == document.getElementById("form_Skill"))
                {
                    cellIndex = i;
                    alert(i);
                    break;
                }
            }
            for(var i = cellIndex; i >= mainTable.tBodies[0].rows[2].length; i++)
            {
                mainTable.tBodies[0].rows[2].cells[i].innerHTML = mainTable.tBodies[0].rows[2].cells[i+1].innerHTML;
            }
            //mainTable.tBodies[0].rows[2].cells[mainTable.tBodies[0].rows[2].length].innerHTML = "";

            // add (skill) to the new index
            var newCell = mainTable.tBodies[0].rows[1].insertCell(0);
            newCell.innerHTML = 1;
            newCell.setAttribute("align", "center");
            var newCell = mainTable.tBodies[0].rows[1].insertCell(1);
            var obj = document.getElementById(theForm).cloneNode(true);
            newCell.appendChild(obj);

            // fixes the id's
            for(var i = 1; i <= newIndex; i++)
            {
                mainTable.tBodies[0].rows[i].cells[0].innerHTML = i;
                mainTable.tBodies[0].rows[i].cells[0].setAttribute("align", "center");
            }

        }
    }
}


function moveDown(theForm)
{

}


//  End -->
</script>
</HEAD>
<BODY>

    <table id="mainTable" name="mainTable" border="1">

        <tr>
            <th>Rank
            </th>
            <th colspan="5">
                <p align="center">The Criteria
            </th>
        </tr>
        <tr>
            <td>
                <p align="center">1
            </td>

            <td>
                <form name="form_Skill" method="post" style="margin: 0; text-align: center;"> 
                    <input type="button" name="Up1" value="Up" onclick="moveUp('form_Skill');"> Skill
                    <input type="button" name="Down1" value="Down" onclick="moveDown('form_Skill');">
                </form>
            </td>

            <td>
                <form name="form_Speed" method="post" style="margin: 0; text-align: center;"> 
                    <input type="button" name="Up2" value="Up" onclick="moveUp('form_Speed');"> Speed
                    <input type="button" name="Down2" value="Down" onclick="moveDown('form_Speed');">
                </form>
            </td>

            <td>
                <form name="form_Age" method="post" style="margin: 0; text-align: center;"> 
                    <input type="button" name="Up3" value="Up" onclick="moveUp('form_Age');"> Age
                    <input type="button" name="Down3" value="Down" onclick="moveDown('form_Age');">
                </form>
            </td>

            <td>
                <form name="form_Strength" method="post" style="margin: 0; text-align: center;"> 
                    <input type="button" name="Up4" value="Up" onclick="moveUp('form_Strength');"> Strength
                    <input type="button" name="Down4" value="Down" onclick="moveDown('form_Strength');">
                </form>
            </td>

            <td>
                <form name="form_Height" method="post" style="margin: 0; text-align: center;"> 
                    <input type="button" name="Up5" value="Up" onclick="moveUp('form_Height');"> Height
                    <input type="button" name="Down5" value="Down" onclick="moveDown('form_Height');">
                </form>
            </td>

        </tr> 
    </table>
</body>
  • 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-24T06:23:43+00:00Added an answer on May 24, 2026 at 6:23 am

    A tr element itself doesn’t have a .length property.

    This:

    for(var i = 1; i >= mainTable.tBodies[0].rows[2].length; i++)
    

    …should get the length from cells instead:

      // right here -----------------------------------v
    for(var i = 1; i >= mainTable.tBodies[0].rows[2].cells.length; i++)
    

    And a <td> doesn’t have an elements property. I assume you intend .children, depending on the browsers you’re supporting.

      // ------------------------------------v
    mainTable.tBodies[0].rows[2].cells[i].children[0]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to check in Transact SQL if a specific column in a table
This is a specific version of this question . I want to check if
I want to check for the existence of a table with a given ID
I want to check if a variable has a valid year using a regular
I have PhoneGap-Android application and I want to check network connection type (No connection,
I want to check out all files in all subdirectories of a specified folder.
I want to check the login status of a user through an ajax request.
I want to check for duplicated words right next to each other, but even
I want to check if a given date is more than a month earlier
I want to check whether the user is viewing my site from a mobile

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.