Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-05-2002, 02:57 AM   PM User | #1
RadarBob
Regular Coder

 
Join Date: Jun 2002
Location: Round Rock, Texas
Posts: 443
Thanks: 0
Thanked 0 Times in 0 Posts
RadarBob is an unknown quantity at this point
Question Cleaner way to reference table data?

Using DOM in IE 5.5 I'm dynamically building table rows containing HTML input elements (checkboxes, text boxes, etc.), one element per TD. Note that these rows are clones - all the element id's for a given element are the same across rows.

Now, For certain things I need to reference two or more elements in the same row. For example, if I check this checkbox I want to enable that text box.

When I click a checkbox I know what it's parent is (the specific TD) and the grandparent (the specific row).

My current effort has me grabbing the table row where the click event happend then, using a pair of nested loops searching for the specific field I want. It works, but it looks like overkill to me. There's gotta be a more elegant way. I don't want to search through all the childNodes when I know which field I want, and it has an id unique to that row.

Can I reference things by their id? In other words I want to say something like this:
thisRow = variable reference to the TR object containing the clicked-on element.

thisRow.theTDid.theCheckBox'sId.checked = false;

or at worst:
thisRow.getElementById("theTDid").getElementById("theCheckBoxId").checked = false;

These don't work but what would? I want to avoid blindly iterating through all the TR's childNodes inside a loop going through all the TD's childNotes, looking for a specific data entry element.
RadarBob is offline   Reply With Quote
Old 11-05-2002, 05:59 AM   PM User | #2
BrainJar
Regular Coder

 
Join Date: Jun 2002
Posts: 185
Thanks: 0
Thanked 0 Times in 0 Posts
BrainJar is an unknown quantity at this point
Quote:
all the element id's for a given element are the same across rows
That's a problem in itself. All IDs should be unique, otherwise you're defeating the purpose. Make them unique and you can access each directly.

You can also access individual TD elements using the built-in array properties for tables:

var el = tableElement.rows[i].cells[j]

so if you know which row you're on and which columns you want, you can just use the index:

tdElement = trElement.cells[n];

TD elements also have a .cellIndex property so if you want the next cell to the right use something like:

var nextTdElement = tdElement.parentNode.cells[tdElement.cellIndex + 1]
BrainJar is offline   Reply With Quote
Old 11-05-2002, 01:21 PM   PM User | #3
RadarBob
Regular Coder

 
Join Date: Jun 2002
Location: Round Rock, Texas
Posts: 443
Thanks: 0
Thanked 0 Times in 0 Posts
RadarBob is an unknown quantity at this point
Quote:
That's a problem in itself. All IDs should be unique, otherwise you're defeating the purpose. Make them unique and you can access each directly.
Well... um.... I'm going to have to go ahead and disagree with you there.

This design is not unlike a data table where a column is a field. The field has only one name. Every record has a field with that same name.

So I guess I'm hoping/wishing there is a valid object referencing scheme that, given a specific row (TR object), now "dot notation" from that row-parent-reference makes the data cell reference-by-id unique.

The array referencing thing might be handy. I'll look into it further. Thanks.
RadarBob is offline   Reply With Quote
Old 11-05-2002, 03:12 PM   PM User | #4
BrainJar
Regular Coder

 
Join Date: Jun 2002
Posts: 185
Thanks: 0
Thanked 0 Times in 0 Posts
BrainJar is an unknown quantity at this point
An HTML table is not the same as a database table, even if it may share some characteristics. The fact remains that the values of ID attributes in an HTML document must be unique (see http://www.w3.org/TR/html401/struct/global.html#adef-id), otherwise they become meaningless.

Most browsers will let you get away with using the same ID just as most can display pages with bad markup. But each is different in how they handle those types of errors. It's not something you want to count on when coding since it will be unpredictable from browser to browser or even version to version.

You might look at the AXIS, HEADERS and SCOPE attributes for table cells (http://www.w3.org/TR/html401/struct/...html#adef-axis) which may be more suitable for what you have in mind.
BrainJar is offline   Reply With Quote
Old 11-05-2002, 05:19 PM   PM User | #5
RadarBob
Regular Coder

 
Join Date: Jun 2002
Location: Round Rock, Texas
Posts: 443
Thanks: 0
Thanked 0 Times in 0 Posts
RadarBob is an unknown quantity at this point
Back to the drawing board

Well below shows how far I got with trying "direct referencing." I got it down to a htmlCollection of table cells (TDs). But the namedItem() property does not work (IE 5.5 windoze) - never mind that Pure JavaScript says it's supported in IE5+.

Code:
   var thisRow = el.parentNode.parentNode;
   var thisRowCells = thisRow.cells;

// the following gives me a "method not supported.." error
   var thisFormatHardCell = thisRowCells.getNamedItem("cellformatHard");

// so does this - even though it is a defined "htmlCollection" property
   var thisFormatHardCell = thisRowCells.namedItem("cellformatHard");
By the way...
The following works. If I have 3 rows, the alert shows 3 fields with the same id and type.
Code:
  var SameName = document.getElementsByName("rptFormatHard");
   rowCellAlert = "NAME       TYPE\n";
   
   for (var x=0; x<SameName.length; x++) {
      rowCellAlert += SameName[x].nodeName + 
      "   " + 
      SameName[x].id + "\n";
   }
      alert (rowCellAlert);
RadarBob is offline   Reply With Quote
Old 11-10-2002, 05:10 PM   PM User | #6
RadarBob
Regular Coder

 
Join Date: Jun 2002
Location: Round Rock, Texas
Posts: 443
Thanks: 0
Thanked 0 Times in 0 Posts
RadarBob is an unknown quantity at this point
The final (compromise) solution - Javascript object

In the FWIW (for what it's worth) department..

Now I'm making an object out of the table row.

Yes, I still have to traverse around a bit to build the row object, but that's "abstracted away" from the functions that do the real work - checking/validating data across a row (data record); makes for very clean coding when working with the data itself.
RadarBob is offline   Reply With Quote
Old 11-13-2002, 05:05 PM   PM User | #7
brothercake
Senior Coder


 
Join Date: Jun 2002
Location: near Oswestry
Posts: 4,508
Thanks: 0
Thanked 0 Times in 0 Posts
brothercake is an unknown quantity at this point
You can always invent your own attributes - they'll mean nothing to the html parser, but you can use them with getAttribute in order to build object sets
brothercake is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:52 AM.


Advertisement
Log in to turn off these ads.