Sam
Sam

Reputation: 267

Pull value from table

Hello I have the following on a page and I would like to pull the value: 36424. the value will change thus Im trying to create a function that will pull it when i call on the function.

    <table cellspacing="0" cellpadding="3" rules="cols" border="1" 
     id="OSDataCount" style="color:Black;background-color:White;border-          
    color:#999999;border-width:1px;border-style:Solid;border-collapse:collapse;">

    <tr style="color:White;background-color:Black;font-weight:bold;">

        <th scope="col">COUNT(*)</th>

    </tr><tr>

        <td>36424</td>

    </tr>

</table>

cheers

Upvotes: 0

Views: 102

Answers (4)

Aleadam
Aleadam

Reputation: 40381

Adding a little detail to previous (correct) answers:

<script type="text/javascript">
  window.onload = function() {
    var table = document.getElementById('OSDataCount');
    var number = table.rows[1].cells[0].innerHTML;
    alert (number);
  }
</script>

Upvotes: 1

Nicole
Nicole

Reputation: 33197

Using jQuery:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>
<script type='text/javascript'>

function alertval() {
    var htmlval = $('#OSDataCount tr:eq(1)>td:first').html();
    var textval = $('#OSDataCount tr:eq(1)>td:first').text();
    alert("Val:" + val);
}

</script>

See http://api.jquery.com/text/, http://api.jquery.com/html/

Using raw JavaScript:

function alertval() {
    var el = document.getElementById("OSDataCount");
    alert("Val:" + el.rows[1].cells[0].innerHTML);
}

Upvotes: 2

mVChr
mVChr

Reputation: 50177

No need for jQuery:

var table = document.getElementById('OSDataCount');
alert(table.rows[1].children[0].innerHTML); // -> 36424

See example →

Upvotes: 2

Marc B
Marc B

Reputation: 360572

var value = document.evaluate('id(OSDataCount)/tr/td', document, null, XPathResult.NUMBER_TYPE);

going off a quick search/reach. Probably won't work, but worth a try.

Upvotes: 0

Related Questions