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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Button Grid Callback 1

Status
Not open for further replies.

xwb

Programmer
Jul 11, 2002
6,828
GB
I've got a grid of buttons which I create dynamically and I'm trying to register the "onclick" for each one. I need to know the position of the button.

The big problem is that getref does not work if the routine referenced has parameters. As a result, I am dynamically creating a bunch of callbacks, one for each button.

Is there a better way of doing this?
Code:
<HTML>
<HEAD>
<TITLE>Button Grid</TITLE>
<META NAME="GENERATOR" Content="Microsoft Visual Studio">
<META HTTP-EQUIV="Content-Type" content="text/html; charset=UTF-8">
<script language="vbscript">
const XMAX = 9, YMAX = 9
dim grid ()

' Grid callback
sub cbGrid (x, y)
   MsgBox cstr(x) & "," & cstr(y)
end sub

sub window_onLoad
   dim x, y, button, cbname, cbbody, newline
   redim grid(XMAX, YMAX)
   for y = 0 to YMAX
      for x = 0 to XMAX
         cbname = "cbGrid" & cstr(x) & cstr(y)
         cbbody = "sub " & cbname & _
                  ": cbGrid " & cstr(x) & "," & cstr(y) & _
                  ": end sub"
         ExecuteGlobal cbbody
         
         set button = document.createElement ("input")
         with button
            .type = "button"
            .value = " "
            .style.width = "20px"
            .onClick = GetRef (cbname)
         end with
         gridcon.AppendChild button
      next
      set newline = document.createElement ("br")
      gridcon.AppendChild newline
   next
end sub
</script>
</HEAD>
<BODY>
<fieldset id="gridcon">
</fieldset>
</BODY>
</HTML>
 
This is how you pass parameters and thus reduce the page to a uniform, single event handler.
[tt]
[red]'[/red]sub cbGrid(x, y)
[blue]sub cbGrid
dim x,y,oelem
set oelem=window.event.srcelement
x=oelem.getAttribute("x")
y=oelem.getAttribute("y")
set oelem=nothing[/blue]
MsgBox cstr(x) & "," & cstr(y)
end sub

sub window_onLoad
dim x, y, button, cbname, cbbody, newline
redim grid(XMAX, YMAX)
for y = 0 to YMAX
for x = 0 to XMAX
[red]'[/red]cbname = "cbGrid" & cstr(x) & cstr(y)
[red]'[/red]cbbody = "sub " & cbname & _
[red]'[/red]": cbGrid " & cstr(x) & "," & cstr(y) & _
[red]'[/red]": end sub"
[red]'[/red]ExecuteGlobal cbbody

set button = document.createElement ("input")
with button
.type = "button"
.value = " "
.style.width = "20px"
[red]'[/red].onClick = GetRef (cbname)
[blue].setAttribute "x",x
.setAttribute "y",y
.attachEvent "onclick",getref("cbGrid")[/blue]
end with
gridcon.AppendChild button
next
set newline = document.createElement ("br")
gridcon.AppendChild newline
next
end sub
[/tt]
 
Brill, thanks for that. I didn't know about setAttribute.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top