473,387 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

innerHTML for a table row problem

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;
}
}

Aug 31 '07 #1
6 5561
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
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;
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
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);
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

Aug 31 '07 #2
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;
}

Aug 31 '07 #3
go****@ratethebuilder.co.uk wrote:
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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 31 '07 #4
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;

Aug 31 '07 #5
go****@ratethebuilder.co.uk wrote:
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;
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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 31 '07 #6
On Aug 31, 9:23 am, goo...@ratethebuilder.co.uk wrote:
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;
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

Aug 31 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jason | last post by:
I have a web page that contains textareas. These are dynamically added via asp when the page loads based on database records. The user also has the ability to add more text areas via innerhtml. ...
2
by: sveinn | last post by:
Hi all, I've read through this group searching for an answear about this problem. Few have come close but not quite what I need. My problem is this: I'm using Ajax to fetch a new table with...
8
by: Clément | last post by:
Hi! I am currently developping a user interface with Ajax/C#/.net. And I am facing a problem with Mozilla, and Firefox. I use the function innerHTML to load a Web UserControl into a div, this...
7
by: Nez | last post by:
Help needed! Hello, I have looked everywhere for a solution to my problem and this is pretty much my last resource. I have created a table in a span with the innerhtml command in my code behind....
3
by: ilia | last post by:
Hi All, I am a newbie in terms of Javascript, I found some code on the net to swap rows in a table using innerHTML, this works fine in Firefox but IE is complaining, after some googling around I...
1
by: huzheng001 | last post by:
I have develop a on-line dictionary website, http://www.stardict.org I meet a problem: Here is two lines of js codes: document.getElementById("wordlist").innerHTML = "";...
3
by: zero0x | last post by:
hi all, i'm trying to create something like window in javascript. there is no problem in firefox, but in microsoft internet explorer i get "Unknown error while working" (this message is in...
5
by: Pratik Patel | last post by:
Hello, I used innerHTML to assign HTML content. but in my HTML page content have also some javascript function and it will run when page load. bu when HTML code assgin thru innerHTML then this...
1
by: Tarik Monem | last post by:
I have been able to successfully retrieve data from an xml file, where the data has been massaged a little bit, to create a table to be retrieved and it is displayed via a document.writeln within a...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.