Getting HTML Elements In JavaScript

Grabbing Multiple Values in HTML

Page 1 of 1

1 Replies - 613 Views - Last Post: 12 February 2010 - 03:51 PM

#1 peter433  Icon User is offline

  • New D.I.C Head

Reputation: -3
  • View blog
  • Posts: 11
  • Joined: 09-February 10

Getting HTML Elements In JavaScript

Posted 12 February 2010 - 06:10 AM

Hello everyone, I recently posted a topic about creating an array in Javascript & making it loop. The problem was quickly solved thanks to a member of the form. I'm having a similar problem, I would like to grab multiple values from this HTML code

<td title='Wait in Lounge;ETD 18:40;Delayed'>Wait in Lounge</td>


& store this in the array I created, as opposed to me defining the array contents manually.

The problem I'm having is grabbing all 3 of these values, I have tried multiple methods including getElementByID, getElementByTagName etc.

Any pointers or help would be greatly appreciated.

Thanks
Peter

Is This A Good Question/Topic? 0
  • +

Replies To: Getting HTML Elements In JavaScript

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1104
  • View blog
  • Posts: 6,913
  • Joined: 07-September 06

Re: Getting HTML Elements In JavaScript

Posted 12 February 2010 - 03:51 PM

I guess it is possible that I am missing something, but why not do something like so:
var tds = document.getElementByTagName("TD");
var tdsWithTitle = [];
var tdInformation = [];
var tmpObj;
var info;
var i;
for(i = 0; i < tds.length; i++){
  if(tds[i].title != ""){
    tdsWithTitle.push(tds[i]);
  }
}
for(i = 0; i < tdsWithTitle.length; i++){
  info = tdsWithTitle[i].title.split(";");
  tmpObj = {};
  tmpObj['title'] = info[0];
  tmpObj['date'] = info[1];
  tmpObj['modifier'] = info[2];
  tmpObj['object'] = tdsWithTitle[i];
  tdInformation.push(tmpObj);
}



At the end of that you will have collected all TDs on the page, narrowed those down to only the ones that have a title attribute set, and then gone through and created an array of objects that have the information from the td's title split apart, as well as a link back to the original object.

NOTE - Untested code.

Hope that helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1