0

I have a case where a html file contains multiple elements with the same ID name.

The table row contains 5 columns of which I need to consider 2,3,4,5 columns data.

<tr id='total_row'>
    <td>Total</td>
    <td>%(count)s</td>
    <td>%(Pass)s</td>
    <td>%(fail)s</td>
    <td>%(error)s</td>
    <td>&nbsp;</td>
</tr>

I have the above code at several places in the file. I need to add the respective values using javascript.

7
  • 2
    Use class insted of id Commented Mar 12, 2014 at 4:26
  • @shijin exactly, you should change total_row as class, and ID should be unique Commented Mar 12, 2014 at 4:27
  • Where do the values come from? What does this have to do with multiple elements having the same ID? Commented Mar 12, 2014 at 4:28
  • Can you please tell why is that you are using an 'id' attribute? If it has something to do with the styling, you can use 'class' instead. Commented Mar 12, 2014 at 4:28
  • first of all id should be unique in complete web page.. but if you want to assign same value to more then one element try with class or ur custom attribute.. dont try with id.. Commented Mar 12, 2014 at 4:28

5 Answers 5

2

An ID is unique in an html page. You can call it THE ID as well wrt a page. You cannot have same ID for two different tags in a single page. But you can use class instead of and ID. Know about it here

So your HTML can be like

<tr class='total_row'>
    <td>Total</td>
    <td>%(count)s</td>
    <td>%(Pass)s</td>
    <td>%(fail)s</td>
    <td>%(error)s</td>
    <td>&nbsp;</td>
</tr>

As an example with jquery you can do something like this,

<!DOCTYPE html>
<html>
<head>
<style>
</style>
</head>

<body>

<table>
    <tr class="one">
        <td></td>
        <td></td>
    </tr>
    <tr class="one">
        <td></td>
        <td></td>
    </tr>
    <tr class="one">
        <td></td>
        <td></td>
    </tr>

</table>

<script src="jquery-1.11.0.min.js"></script>
<script>
$(document).ready(function() {
    $(".one").eq(0).find('td').eq(0).html("I'm tracked");
    // get 1st tr and get first td
    $(".one").eq(1).find('td').eq(1).html("I'm tracked");
    // get 2nd tr and get second td
    $(".one").eq(2).find('td').eq(0).html("I'm tracked");
    // get 3rd tr and get first td
});
</script>
</body>
</html>

But I guess this approach can be tedious.

Sign up to request clarification or add additional context in comments.

1 Comment

@RobG I was recommending him the library. I don't think recommending is eligible for down voting(I don't know frankly). Still if you say me that it is then I'll remove my answer. :)
1

Id should be unique and if you use the same id, javascript code refers only the first element. but if you still want to use same id than you may try the below code:

$(function(){
   $('[id="total_row"]').each(function(){//run for every element having 'total_row' id
      var $this = $(this);
      $this.find('td').eq(1).text() //to get second column data
      $this.find('td').eq(1).text('dummy text') //to set second column data
   });
});

1 Comment

Since this is the table row, then this.cells[i] is the i-th cell.
0

You can use XHTML:

<p id="foo" xml:id="bar">

Through XHTML you can apply similar ID to multiple Controls.

Similar questions can be found here:

http://www.c-sharpcorner.com/Forums/

Comments

0

While duplicate IDs are invalid, they are tolerated and can be worked around. They are really only an issue when using document.getElementById.

I'll guess that the table looks like:

<table id="t0">
  <tr>
    <td>-<th>count<th>Pass<td>Fail<td>Error<td>
  <tr>
    <td>-<td>1<td>1<td>0<td>0<td>&nbsp;
  <tr>
    <td>-<td>1<td>1<td>0<td>0<td>&nbsp;
  <tr id='total_row'>
    <td>Total<td><td><td><td><td>
  <tr>
    <td>-<td>1<td>1<td>0<td>0<td>&nbsp;
  <tr>
    <td>-<td>1<td><td>1<td>0<td>&nbsp;
  <tr>
    <td>-<td>1<td><td>0<td>1<td>&nbsp;
  <tr id='total_row'>
    <td>Total<td><td><td><td><td>
</table>

<button onclick="calcTotals();">Calc totals</button>

If that's correct, then a function to add each sub–section can be like:

function calcTotals(){
  var table = document.getElementById('t0');
  var rows = table.rows;
  var row, totals = [0,0,0,0];

  // For every row in the table (skipping the header row)
  for (var i=1, iLen=rows.length; i<iLen; i++) {
    row = rows[i];

    // If it's a total row, write the totals and
    // reset the totals array
    if (row.id == 'total_row') {
      for (var j=0, jLen=totals.length; j<jLen; j++) {
        row.cells[j+1].innerHTML = totals[j];
        totals[j] = 0;
      }

    // Otherwise, add values to the totals
    } else {
      for (var k=0, kLen=totals.length; k<kLen; k++) {
        totals[k] += parseInt(row.cells[k + 1].innerHTML) || 0;
      }
    }
  }
}

Comments

0

In addition to using classes, which works but feels kind of icky to me, one can also use data-* attributes.

<tr class='total_row' data-val-row-type="totals-row">
    <td>Total</td>
    <td>%(count)s</td>
    <td>%(Pass)s</td>
    <td>%(fail)s</td>
    <td>%(error)s</td>
    <td>&nbsp;</td>
</tr>

Then, in your script (jQuery syntax -- querySelectorAll has a similar syntax)

var $totalsRows = $("[data-val-row-type='totals-row']);

When you are in a team with a separate UI designer, this keeps the UI guy from ripping out and changing your class names to fix the new design layout and it makes it quite clear that you are using this value to identify the row, not just style it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.