Forum Moderators: open

Message Too Old, No Replies

DOM Tables . alternating venue menu.

Kinda hard to explain but...

         

Conscientious Reject

4:58 am on Sep 12, 2005 (gmt 0)

10+ Year Member



Please look over this for my problem. Any suggestions are greatly appreciated. I know I am probabully way off but I need help before I lead myself astray.

<HTML>
<HEAD>
<TITLE>Welcome to My Exhibitions</TITLE>
<SCRIPT LANGUAGE="JavaScript">

<!-- Begin
function addRow(){
var argv = addRow.arguments;
cal argc = argv.length;
for (var i = 0; i < argc; i++) {
var displayButton = document.getElementById(displayButton);
var row = document.createElement("tr")
var c_TD1 = document.createElement("td")
c_TD1.appendChild(document.createTextNode (argv[1])
row.appendChild(c_TD1);
if (c_TD1 > 1) {
displayButton.appendChild(row);
}
displayButton.appendChild(row);
}
}

// End -->
</script>

</HEAD>
<BODY>
<p>After Clicking on the following link. I want the visitor to have

access to varying buttons, based on their selection of of venue. The

problem I am having is that the number of these buttons will not be

static from venue menu to venue menu. The following button is an

example of one of these venue selections.
<br><br><a onclick="addRow('pic1', 'pic2', 'pic3',

'pic4');">venue1Link</a>

<TABLE WIDTH=375 id="displayButton">
</TABLE>

<p>Here is the problem. It doesn't work. But if it did it would

render a table that looks like this.

<TABLE WIDTH=375>
<TR>
<td>pic1</td><td>pic2</td></tr>
<td>pic3</td><td>pic4</td></tr>
</TR>
</TABLE>

<p>I want to pass the the name of each button through the addRow()

parameters.

</BODY>
</HTML>

Bernard Marx

7:34 am on Sep 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There were a few typos:
- missing quotes inside getElementById
- a fantom operator, cal.

The algorithm was a little messed up too.

(not a big deal) The arguments array as a property of the function is deprecated in Javascript. It is now referenced globally.

The rows must be added, not to the TABLE, but to the TBODY element, which is there even if it's not in the source code. (This always gets me too).

It is apparently quicker to build a table 'outwards', rather than building each row then adding it.

--HTML--------------------------

<a href="#" onclick="addRow('pic1', 'pic2', 'pic3','pic4');return false;">

--JS----------------------------

function addRow()
{
var body = document.getElementById('displayButton')
.getElementsByTagName('tbody')[0];
var row = body.appendChild( document.createElement('tr') );

for ( i=0, cell; i < arguments.length; i++)
{
cell = row.appendChild( document.createElement('td') );
cell.appendChild( document.createTextNode( arguments[i]) );
}
}

Conscientious Reject

7:20 am on Sep 14, 2005 (gmt 0)

10+ Year Member



Thanx Marx,
This totally makes sense. I am a bit out of sorts lately. I haven't really attempted this (time is a commodity for me.) but I will in the next few days and let you know if it works. Still, even with just your generous insight I am sure to succeed.

Conscientious Reject

3:06 am on Sep 15, 2005 (gmt 0)

10+ Year Member



Done ...
Amazingly enough you tackled it right on the money. Although, it didn't work at first because the variable cell needed to be declared globally.

var cell = row.appendChild(document.createElement('td') );

but who cares...

By the way why do you use the "return false" at the end of the event handler?

Gratefully,
Mitchell

Conscientious Reject

4:58 am on Sep 15, 2005 (gmt 0)

10+ Year Member



Hello,
It's me again and sure hope that people are still viewing this forum. For I am in a pickle. Okay ... Everything works fine with this code. Just as I had hoped, but the small mountians only cloaked the monsters behind them.

What I want here is to create styles, and set attributes to the new classes. What's wrong with this?

function addRow() {
var body = document.getElementById('displayButton')

.getElementsByTagName('tbody')[0];
var row = body.appendChild(document.createElement('tr') );
for ( i=0, cell; i < arguments.length; i++) {
var nameMe = document.createElement("td");
nameMe.setAttribute("name","gallery_Item[" + i + "]");
nameMe.onClick(picture.src=display[" + i + ];
changeDescript(myPic_name[" + i + "], myPic_descript[" + i + "]));
var cell = row.appendChild(nameMe);
cell.appendChild(document.createTextNode( arguments[i]) );
}
}

Bernard Marx

7:48 am on Sep 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



the variable cell needed to be declared globally.

var cell = row.appendChild(document.createElement('td') );

Youre half-right there. It should hav been declared (not globally though). In fact I attempted to
but forgot the 'var'. At the top of the loop:

for ( [b][blue]var[/blue][/b] i=0, cell; i < arguments.length; i++) 


Inside the new function, you've made a little mistake in the loop.
A missing quote:

nameMe.onClick(picture.src=display[" + i + [b][blue]"[/blue][/b]];

There's no need to swap variables half-way through.
So.

Are you sure you want to set a "name" attribute for a TD element?

for (var i=0, cell; i < arguments.length; i++) 
{
cell = document.createElement("td");
/* name attribute for a TD? */
cell.setAttribute("name","gallery_Item[" + i + "]");
cell.onClick(picture.src=display[" + i + "];
changeDescript(myPic_name[" + i + "], myPic_descript[" + i + "]));
row.appendChild(cell);
cell.appendChild(document.createTextNode( arguments[i]) );
}

Bernard Marx

7:57 am on Sep 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



and..

By the way why do you use the "return false" at the end of the event handler?

The link has href="#" ("top of page"). return false stops the action being entered in the history, and the page jumping if it is scrolled down.

Conscientious Reject

9:18 pm on Sep 15, 2005 (gmt 0)

10+ Year Member



there seems to still be some problems I got the code to actually get past the errors with this...

function addRow() {
var body = document.getElementById('displayButton') .getElementsByTagName('tbody')[0];
var row = body.appendChild( document.createElement('tr'));
for (var i=0, cell; i < arguments.length; i++) {
cell = document.createElement("td");
/* name attribute for a TD? */
cell.setAttribute("name","gallery_Item[" + i + "]");

/*there is a missing ')' somewhere here. I can't figure it out. error says that it is expected.*/
/*Actually, I can get passed this error by changing the ';' to '&&'*/
cell.onClick(picture.src=display[" + i + "] &&
changeDescript(myPic_name[" + i + "], myPic_descript[" + i + "]));

row.appendChild(cell);
cell.appendChild(document.createTextNode( arguments[i]) ); }
}

but now it wont work.

Bernard Marx

10:08 pm on Sep 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This statement isnt what you intend:

cell.onClick(picture.src=display[" + i + "] &&
changeDescript(myPic_name[" + i + "], myPic_descript[" + i + "]) );

I think you are trying to assign an onclick handler, but are calling an imaginary onClick method
with all that as arguments. Read
this thread
[webmasterworld.com] it may help.

The statement is also syntactically messed up - look at the quotes. So it will cause an error from the getgo.

I'm not sure I can say more until I know more.

Conscientious Reject

2:55 pm on Sep 16, 2005 (gmt 0)

10+ Year Member



Okay ... Okay ...
I'll attempt to break it down a bit.

First of all I dont need to name the table cells anymore now that the arrguments can define the text.

The user will have the option to change up the gallery venue with out loading a new page. Instead the buttons to the gallery images actually change, both thier text and the image they call. This is why I need to use arrays for just about every argument they pass. Basically, when the user decides to change the gallery venue, then buttons will be loaded into the <tbody>. We started out actually giving the user the option to create the following tbody.

<tr><td>pic1</td><td>pic2</td></tr>

but what I want is this. What I need to know is how to write the following javascript properly

cell.onClick = "picture.src='display[" + i + "]'; changeDescript(myPic_name[" + i + "], myPic_descript[" + i + "])";

where it would render a tbody comparible to:

<tr><td name="gallery_Item[0]" onclick="picture.src=display[0]; changeDescript(myPic[0], myPic_descript[0])">pic1</td>

<td name="gallery_Item[1]" onclick="picture.src=display[0]; changeDescript(myPic[1], myPic_descript[0])">pic1</td>

with the user having the option to reload the tbody and render something that looks exactly like the above (depending on how many arguments there are - images)
but instead the actual arrays change.

FYI:
where

display[0] = beforeLunch_pic.src;
myPic_name[0] = beforeBrunch;
myPic_descript[0] = beforeBrunch_describe;

now the arrays change once the user has opted to change the gallery venue.

display[0] = shadow_pic.src;
myPic_name[0] = afistFull;
myPic_descript[0] = afistFull_describe;

What I need to know is how to write the following javascript properly where it would render a tbody comparible to: