select matching <tr>s by title

javascript , xhtml Ireland
  • 11 years ago

    Hi. Could anyone please assist. I have a table with rows, some with the same title. When I click on one of these rows, I want all the rows with matching titles backgrounds to turn red.

    See code for table below:

    <tr title="1"><td>1</td>"><td>Item 1</td></tr>
    <tr title="1"><td>1a</td>"><td>Item 1a</td></tr>
    <tr title="2"><td>2</td>"><td>Item 2</td></tr>
    <tr title="3"><td>3</td>"><td>Item 3</td></tr>


  • 11 years ago

    This should do it:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

    ""> <html xmlns="">


    <title></title> <script type="text/javascript">

    window.onload = function() {

    var mainTable = document.getElementById("TableContainer")
    ,selectionCriterion = "1" /* what's inside the "title" attribute */
    ,selectedRowColor = "#FF0000" /* the color to apply to the selected row */
    ,AllRows = mainTable.getElementsByTagName("tr");

    for (var i = 0; i < AllRows.length; i++) {

    var selectedRows = AllRows[i];

    if (selectedRows.getAttribute("title") == selectionCriterion) { = selectedRowColor;







    <table id="TableContainer">

    <tr title="1"><td>1</td><td>Item 1</td></tr>

    <tr title="1"><td>1a</td><td>Item 1a</td></tr>

    <tr title="2"><td>2</td><td>Item 2</td></tr>

    <tr title="3"><td>3</td><td>Item 3</td></tr>

    </table> </body>




Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Every language has an optimization operator. In C++ that operator is ‘//’”