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

  • Home
  • SEARCH
  • 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 8787859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:59:56+00:00 2026-06-13T21:59:56+00:00

I am trying to get all <TR> tags within <TABLE> tag in an array

  • 0

I am trying to get all <TR> tags within <TABLE> tag in an array and I am trying to refer it like arrayname[1], arrayname[2] . Then I am looking forward to set a unique title attribute to <TR> tag’s, using idArr[1].setAttribute("title", "myClass");

Here is the code I have written, which does not works. Can anyone guide me what’s wrong in it !!

<html>
<head>
<script type="text/javascript" >
var idArr = [];
var formm=document.getElementById("form1");
var trs = formm.getElementsByTagName("tr");
alert(trs.length);
for(var i=0;i<trs.length;i++)
{
   idArr.push(trs[i].id);
   alert(idArr.innerText);
}
idArr[1].setAttribute("title", "myClass");
</script>

</head>
<body>
<form id="form1">
<table border="1">
<tr>
<td>One</td>
<td>Two</td>
</tr>
<tr>
<td>ABC</td>
<td>EFG</td>
</tr>

</table>
</form>
</body>
</html>
  • 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-13T21:59:58+00:00Added an answer on June 13, 2026 at 9:59 pm

    A couple considerations:

    • The elements do not exist when the code runs,
    • You don’t need the Array for what you’re doing.

    <html>
    <head>
        <title>My Page</title>
    </head>
    <body>
        <form id="form1">
            <table border="1">
            <tr>
                <td>One</td>
                <td>Two</td>
            </tr>
            <tr>
                <td>ABC</td>
                <td>EFG</td>
            </tr>
    
            </table>
        </form>
    
     <!-- Now your script will not run until the elements above are loaded-->
        <script type="text/javascript" >
        var formm=document.getElementById("form1");
        var trs = formm.getElementsByTagName("tr");
        for(var i = 0, len = trs.length; i < len; i++) {
            trs[i].setAttribute("title", "myClass");
        }
        </script>
    
    </body>
    </html>
    

    So I got rid of the extra Array code, and I moved the script to the bottom of the page so that it doesn’t run until the DOM above it has loaded.

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

Sidebar

Related Questions

I would like to extract all img tags that are within an anchor tag
I am trying to get the src of all the script tags and i
I am trying get all html links within a string and replace them using
I am trying get all html links within a string and replace them using
I'm trying to get all the found elements within a <div> , but by
I am trying to get all css link from html like this segment of
I'm trying to get all of the following tags inside the body (INPUT, A,
I'm trying to parse an html document with Jsoup to get all heading tags.
Following is my javascript program. I am trying to get all child tags of
iam trying to get all object's xpath's from loaded page via selenium something similar

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.