Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem adding table rows dynamically with id

Status
Not open for further replies.

NeeNee

Programmer
Apr 2, 2002
97
CA
I am trying to create a function to add rows dynamically to a table. 2 rows need to be added to the table each time.

The first row has a check box which determines is the second row is visible or not. I am creating the other rows when the data is first loaded using ASP and the javascript function works great to show or hide the table rows.

Each row is created with an id which is passed to the function. In ASP, I can create table rows with ID no problem. In javascript it get an error.

Here is the line of code that creates the row:
currentRow = document.createElement("TR");
currentRow.setAttibute("id", "rowOrgName");

When the function runs, the setAttributes line generates the error "object does not support this property or method".

I am at a loss. I am new to javascript. I am also using the style/display tag to show or hide a table row. Is there a better way?

Thanks

Denis
Programmer, Canada
 
Building tables dynamically is a "special case" where using createElement can provide unpredictable results in some browsers. Your best bet is to use the "insertRow" and "insertCell" methods available. See here:


and here:


for more info.

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
I may be mistaken (perhaps this is the 'special case' that Dan refers to) but after creating an element isn't it then necessary to insert it?

I'm also assuming the typo is from being retyped in here?

Code:
currentRow.setAtt[b][COLOR=red]r[/color][/b]ibute("id", "rowOrgName");


And finally,
See thread216-1304109. I had a different problem yesterday but the symptom was identical to yours. I was using a function to allow me to execute multiple functions when the window.onload event fired. It was to do with passing parameters to the function.

<honk>*:O)</honk>
Designease Ltd. - polyprop folders, ring binders and creative presentation ideas
Earl & Thompson Marketing - Marketing Agency Services in Gloucestershire
 
It's not strictly necessary to insert / append an element before working with it - in fact, in most cases, it is quicker to manipulate it before adding it to the DOM.

I've only ever found one case where I needed to add an element to the DOM before manipulating it... and that went back to the days of NN 4, and IE 5 on Mac OS 9 - so I doubt it's an issue any more.

Could be onto something with the typo, though. Maybe I need new glasses! [bigglasses]

Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Well do I feel stupid!!!

Thanks for pointing out the typo.

Now that portion works great, but I do have a problem with assigning a function name to the onclick event to my select. From what I have read, this is what I have.

mySelect = document.createElement('select')
mySelect.setAttribute(onclick, 'UpdateList()')

Also, is there a tool out there to see all the DOM after a javascript object has been created. I tried View Source, but it only shows the original page, but not the new table rows.

Thanks


Denis
Programmer, Canada
 
Thanks Dan,

I was using the wrong event and my syntax was also wrong.

Denis

Denis
Programmer, Canada
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top