J. Davidson
J. Davidson

Reputation: 3307

Reading element by Id

HI I have following table

<table>
   <tr id="">
      <td class="row_class" id="row_id_1">233.00</td>
      <td class="row_class" id="row_id_2">2300</td>
      <td class="row_class" id="row_id_3">3.00</td>
      <td class="row_class" id="row_id_4">4.00</td>
      <td class="main" id="row_id_5">5.00</td>
      <td class="main" id="row_id_6">112.00</td>
   </tr>
</table>

I am trying to access row_class so I can add some formatting to the number. The code I have to access

   function formatCurrency() {
      alert();
      var num = document.getElementById('table td .row_class').value;
      var cNum = document.getElementById('table td .main').value;
      document.getElementById( val1 ).value = processformat( num );
      document.getElementById( val2 ).value = processformat( cNum );
   }

I get following error when i run it

TypeError: document.getElementById(...) is null
var num = document.getElementById('table td .totalS').value;

I am trying to figure out the way to fix this error as well I am not sure if I need to apply .each function to apply the formatting to all elements?

Upvotes: 0

Views: 68

Answers (3)

Chris Wijaya
Chris Wijaya

Reputation: 1286

You have to understand that your code is trying to access a html tag which is given the 'id' attribute of whatever you define in the 'getElementById()' parameter.

var num = document.getElementById('table td .row_class').value;

Your code above is saying, get me the value of a html tag with 'table td .row_class' id and put it into 'num' variable which of course return null because you don't have any tag with id of 'table td .row_class'.

Two things you need to fix:

  • getElementById() parameters, and
  • the innerHTML property instead of the value property

Your fix would be something like this:

function formatCurrency() {
        alert();
        var num = document.getElementById('row_id_1').value;
        var cNum = document.getElementById('row_id_5').value;
        document.getElementById('row_id_1').innerHTML= processformat(num);
        document.getElementById('row_id_5').innerHTML= processformat(cNum);
    }

Upvotes: 0

sangram parmar
sangram parmar

Reputation: 8726

Try this:

 function formatCurrency() {
            alert();
            var num = $('table td.row_class').html();
            var cNum = $('table td.main').html();
            document.getElementById(val1).value = processformat(num);
            document.getElementById(val2).value = processformat(cNum);
        }

Upvotes: 0

Amit
Amit

Reputation: 15387

Try this, You are trying to get value using document.getElementById with className, it is wrong.

function formatCurrency() {
        alert();
        var num = document.getElementById('row_id_1').value;
        var cNum = document.getElementById('row_id_5').value;
    }

In Jquery

   function formatCurrency() {
        alert();
        var num = $('#row_id_1').val();
        var cNum = $('#row_id_5').val();
    }

Upvotes: 3

Related Questions