0

it's me again, and I found something, but something I missing. I want to get value from id and I can't do that.

<script type="text/javascript">
     var chart;
        var legend;

        var chartData = [{
            name: "NPukelis",
    **value: document.getElementById("2").innerHTML;**
        }, ....

<td id="2" >19</td> I want to get number from this one

2 Answers2

0

Why can't I have a numeric value as the ID of an element?

Change your id to a proper valid id. Assigning numeric id does not give your an error,but still is not a good practice.You will keep looking for error and it will bug all of your time.

Try this instead

 <td id="item2" >19</td>

document.getElementById("item2").innerHTML
Community
  • 1
  • 1
HIRA THAKUR
  • 17,189
  • 14
  • 56
  • 87
  • Ok but i think that line value: document.getElementById("2").innerHTML; is not correct. – Stasys Razma Jul 30 '13 at 14:36
  • it is perfectly correct...what are you doing ...try console..what error are you getting?? – HIRA THAKUR Jul 30 '13 at 14:39
  • where i should try it? i am absolutely new. When I'am trying to get values from table with this getElementById my chart not shows on screen. – Stasys Razma Jul 30 '13 at 14:48
  • i would suggest you to go step by step...dont create a array which has an object inside it,then you access it.. `var chartData = [{a:1,b:1}]`...instead store the values directly..`var value=document.getElementById("item2").innerHTML`; – HIRA THAKUR Jul 30 '13 at 14:54
0

I briefly ran into an issue myself trying to reproduce your problem - the issue being that I didn't have <table> tags around the <td> field, at which point the field simply wasn't recognized and I could not get its contents by ID. But as you can see at the following fiddle http://jsfiddle.net/eup3y/3/ using a number for the ID, whilst not recommended, is not the problem (if you are using HTML5 - for older browsers that's not true as the spec required the ID to start with a letter).

In order to debug, I recommend that you break this into smaller pieces (that usually is a good idea):

  • Can you access the value from a "regular" script - use for example

    alert(document.getElementById("2").innerHTML);

If you can't, then make sure that you have valid HTML in the region around your table. In particular make sure there are opening and closing <table> tags around it. If the HTML didn't parse correctly, you can't extract the elements by ID

  • Can you create the chart element that you want when you hard code the value (instead of looking up the value 19 as you are doing now)

My hunch is that your HTML doesn't parse properly. If you open the page with an error console open, you will find the error quickly I imagine.

Floris
  • 45,857
  • 6
  • 70
  • 122