3 Replies - 474 Views - Last Post: 09 April 2013 - 11:30 AM

#1 Zedex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-April 13

Accessing td elements values.

Posted 08 April 2013 - 01:05 PM

Hi, I'm new to Javascript and wondering how to access individuals td elements.

I want to sum up all the values inside td's under the td class London and the same with Paris. So I would have two variables one with value 81 and one with value 103.

I can't really figure out to do this. Should I get all td elements using document.getElementById("td") and make a forloop to iterate over it and make some kind of condition inside it? Or what is the appropriate way to do this? thanks

            <tr>
                <td class = "city">London</td>
                <td>6</td>
                <td>18</td>
                <td>12</td>
                <td>22</td>
                <td>23</td>
            </tr>
            <tr>
                <td class = "city">Paris</td>
                <td>24</td>
                <td>25</td>
                <td>18</td>
                <td>19</td>
                <td>17</td>
            </tr>




Is This A Good Question/Topic? 0
  • +

Replies To: Accessing td elements values.

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3480
  • View blog
  • Posts: 10,033
  • Joined: 08-June 10

Re: Accessing td elements values.

Posted 08 April 2013 - 01:30 PM

View PostZedex, on 08 April 2013 - 10:05 PM, said:

Should I get all td elements using document.getElementById("td") and make a forloop to iterate over it and make some kind of condition inside it?

much too complicated. get all <td>s within a <tr> (hint: e.g. getElementsByTagName())
Was This Post Helpful? 0
  • +
  • -

#3 Zedex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-April 13

Re: Accessing td elements values.

Posted 09 April 2013 - 11:26 AM

Hi, I'm trying to do something like u said but can't seem to make it work I want look at the first child node of the tr element to see if it equals London and if it does i want to get the values under it and do the same for paris.


		var tbrows = document.getElementsByTagName("tr");


		for (var i = 0; i < index.length; i++) {
			
		if(tbrows[i].firstChild.innerHTML == "London")
		{

			
		};


Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3480
  • View blog
  • Posts: 10,033
  • Joined: 08-June 10

Re: Accessing td elements values.

Posted 09 April 2013 - 11:30 AM

where do you define the variable index?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1