Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / August 2007



Tip: Looking for answers? Try searching our database.

innerHTML for a table row problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
google@ratethebuilder.co.uk - 31 Aug 2007 14:49 GMT
I am having trouble with the following function that I am modifying
from www.isocra.com. I am trying to change the ID of a hidden form
object inside a table row. I have used alert() to test where it breaks
and it seems to be on the last line of the function where I am trying
to place the new text back into the table row. Anyone any ideas where
I a going wrong as I'm not a great Javascript coder.

Thanks in advance.

this.onDrop = function(table, droppedRow) {
     var rows = table.tBodies[0].rows;
     var rowsCount = parseInt(rows.length);

     for (var x = 0; x <= rowsCount - 1; x++)
     {
         //Get string contents of row
          var curRowString = rows[x].innerHTML;

         //Find where ID tag is
          var IDStart = curRowString.indexOf("ID");

         //Find > immediately after ID tag
          var IDEnd = curRowString.indexOf(">", IDStart);

         //Get ID value
          var IDVal = curRowString.substring(IDStart+5,IDEnd)

         //Replace old ID with new ID
          var oldID = "ID" + IDVal;
          var newID = "ID" + (x+1);
          var newRowString = curRowString.replace(oldID, newID);

          //*********** Crashes on line below ***************
          rows[x].innerHTML = newRowString;
     }
}
RobG - 31 Aug 2007 15:57 GMT
On Aug 31, 11:49 pm, goo...@ratethebuilder.co.uk wrote:
> I am having trouble with the following function that I am modifying
> fromwww.isocra.com. I am trying to change the ID of a hidden form
[quoted text clipped - 7 lines]
> this.onDrop = function(table, droppedRow) {
>       var rows = table.tBodies[0].rows;

If you want all the rows in the table, consider using the table's rows
collection:

 var rows = table.rows;

>       var rowsCount = parseInt(rows.length);

rows.length is defined as a number, there is no need for parseInt
unless you know of some broken implementations that need it.

>       for (var x = 0; x <= rowsCount - 1; x++)

Why not:

 for (var x = 0; x < rowsCount; x++)

>       {
>           //Get string contents of row
[quoted text clipped - 13 lines]
>            var newID = "ID" + (x+1);
>            var newRowString = curRowString.replace(oldID, newID);

I'm surprised that works at all, but apparently it does.

>            //*********** Crashes on line below ***************
>            rows[x].innerHTML = newRowString;

Never use innerHTML to modify a table.  You can use it to write an
entire table, or the contents of a cell, but nothing in between. Use
DOM methods.

It seems that you don't know what the ID is (else you'd just use
getElementById), there are other ways of finding form controls or for
searching for particular types of elements:

  rows[x].getElementsByTagName('input')

is one, then iterate over the returned collection to find what you
want.

--
Rob
google@ratethebuilder.co.uk - 31 Aug 2007 16:53 GMT
Thanks for your help.

So do you reckon I could replace all the 'for' code with:

var y = 1
for (var x = 0; x < rowsCount; x++)
{
    rows[x].getElementsByTagName('hidden').ID = y;
    y += 1;
}

Or

var y = 1
for (var x = 0; x < rowsCount; x++)
{
    rows[x].getElementsByTagName('hidden')[0].ID = y;
    y += 1;
}
Martin Honnen - 31 Aug 2007 17:20 GMT
>     rows[x].getElementsByTagName('hidden').ID = y;

getElementsByTagName returns a node list, not a single node. And
'hidden' is not a HTML element tag name, 'input' is for instance.

Signature

    Martin Honnen
    http://JavaScript.FAQTs.com/

google@ratethebuilder.co.uk - 31 Aug 2007 17:23 GMT
So I need something similar to the below, except the below doesnt
work:

var y = 1
for (var x = 0; x < rowsCount; x++)
{
       rows[x].getElementsByTagName('input')[0].ID = y;
       y += 1;
Martin Honnen - 31 Aug 2007 17:34 GMT
> So I need something similar to the below, except the below doesnt
> work:
[quoted text clipped - 3 lines]
> {
>         rows[x].getElementsByTagName('input')[0].ID = y;

We don't know which input's id you want to change, you will have to show
us a sample of the row content. Rob suggested you loop through the input
elements and look for one with type being 'hidden'.
And the property name is 'id' and not 'ID', so once you have the right
input use .id = y and not .ID = y.

Signature

    Martin Honnen
    http://JavaScript.FAQTs.com/

David Golightly - 31 Aug 2007 19:43 GMT
On Aug 31, 9:23 am, goo...@ratethebuilder.co.uk wrote:
> So I need something similar to the below, except the below doesnt
> work:
[quoted text clipped - 4 lines]
>         rows[x].getElementsByTagName('input')[0].ID = y;
>         y += 1;

http://www.w3.org/TR/html4/types.html

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be
followed by any number of letters, digits ([0-9]), hyphens ("-"),
underscores ("_"), colons (":"), and periods (".").

-David
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.