BUG??? or am I doing something wrong... 
Author Message
 BUG??? or am I doing something wrong...

Hello,

I'm using the DOM through VBScript to insert different types of objects
(e.g. INPUT type=text).
I assign it an "id" so that I can trap events on the object.  However,
the events are never raised.

For example...

Say I have a table defined in an HTML page with and there is a cell with
an ID of 'tdMyInput'
Somewhere in my script I have the following

document.all("tdMyInput").innerHTML = "<INPUT type=text
id='MyInput'></INPUT>"
...

As well, I have the following defined:

function MyInput_onchange()
    msgbox "MyInput has been changed."
end function

Well, this never ever gets called.  I tried using 'onclick' and it too
never gets called, but I can capture it with

function document_onclick
     if window.event.srcElement.id = "MyInput" then
        ' do something
    end if
end function

So I tried the same with document_onchange, but nope it doesn't work.

Is there something I'm unaware of or is this a bug?  It seems like using
innerHTML doesn't "link-into" the
DOM so events don't seem to be handled...

I really need a solution/workaround to this as I build a list using
VBScript and DOM but still need to react
to events occuring in list entries.

Thank you,

Mark Gabriel



Mon, 14 May 2001 03:00:00 GMT  
 BUG??? or am I doing something wrong...
It's not a bug, it's a tricky concept.  Check out the documentation on DHTML
at http://www.microsoft.com/wprkshop/  There is a great listing of the DOM
from IE.  You will need to use the methods of the table called insertRow and
insert Cell (a method of a row).  As far as dynamically handling the events,
you can set the onClick and onMouseOver, etc events in VBScript...

The following code has not been checked for correctness, it is merely an
impromptu example.... check the SiteBuilder Workshop for more detailed
information.

<table id=myTable>
</table>

<script language="VBScript">
Sub window_onLoad()
    Dim newRow
    Dim newCell
    Set newRow = document.all("myTable").insertRow
    newRow.style.backgroundColor = "red"
    Set newCell = newRow.insertCell
    newCell.onClick = "doCall(Me)"
    newCell.innerHTML = "<input type=text name=fred value=""test data""
id=myFredTextBox>"
End Sub
Sub doCall(theCell)
    theCell.innerHTML = "you clicked this cell"
End Sub
</script>

See how far that gets you....

Michael Earls

Quote:

>Hello,

>I'm using the DOM through VBScript to insert different types of objects
>(e.g. INPUT type=text).
>I assign it an "id" so that I can trap events on the object.  However,
>the events are never raised.

>For example...

>Say I have a table defined in an HTML page with and there is a cell with
>an ID of 'tdMyInput'
>Somewhere in my script I have the following

>document.all("tdMyInput").innerHTML = "<INPUT type=text
>id='MyInput'></INPUT>"
>...

>As well, I have the following defined:

>function MyInput_onchange()
>    msgbox "MyInput has been changed."
>end function

>Well, this never ever gets called.  I tried using 'onclick' and it too
>never gets called, but I can capture it with

>function document_onclick
>     if window.event.srcElement.id = "MyInput" then
>        ' do something
>    end if
>end function

>So I tried the same with document_onchange, but nope it doesn't work.

>Is there something I'm unaware of or is this a bug?  It seems like using
>innerHTML doesn't "link-into" the
>DOM so events don't seem to be handled...

>I really need a solution/workaround to this as I build a list using
>VBScript and DOM but still need to react
>to events occuring in list entries.

>Thank you,

>Mark Gabriel



Mon, 14 May 2001 03:00:00 GMT  
 BUG??? or am I doing something wrong...
A lot of people are running into this problem.  Pasted below is a solution I
sent someone else who was having a similar problem:

Here is the problem:  You can't set an event handler to run "code"
programmatically (we believe it's because it doesn't get recognized by the
scripting engine).  However, you CAN set an event handler to a function
pointer.  So, when you say

MyControl.onchange = beep;

you're setting the onchange event to your beep function pointer.  However,
when you try and say

MyControl.onchange = "alert('hello')"

you're giving the onchange event a line of code to run, which you can't do.
What this means is that as long as none of your functions have any
parameters, you can set the event handlers to your function pointers no
problem.  (Also, you have to write all of your functions in JScript, since
VBScript doesn't support function pointers.)

So what do you do if you want to give your functions parameters, or write
code in VBScript?  Fear not, there is a workaround!  :-)

For example:

MyControl.onchange = "MyFunc('string')";
MyControl.outerHTML = MyControl.outerHTML;

The first line changes the onchange event.  However, we just said, this
doesn't work.  The second line will only replace the tag by itself.  But, as
far as the scripting engine is concerned, this is a new tag, and will
register the new event handlers.  This will now handle functions with
parameters, and functions/subs written in VBScript.

Of course, this doesn't apply DIRECTLY to your problem, because this is
concerned with changing events for elements that already exist, whereas you
are trying to create new elements.  But I'm betting that when you change the
innerHTML property, if you were also to add in something like
"onchange='MyInput_onchange()'" it would work.

Let us know what happens, or if you find another solution.

--
David Hunter and Andrea Derouin
MediaServ Information Architects
http://www.MediaServ.com

Quote:

>Hello,

>I'm using the DOM through VBScript to insert different types of objects
>(e.g. INPUT type=text).
>I assign it an "id" so that I can trap events on the object.  However,
>the events are never raised.

>For example...

>Say I have a table defined in an HTML page with and there is a cell with
>an ID of 'tdMyInput'
>Somewhere in my script I have the following

>document.all("tdMyInput").innerHTML = "<INPUT type=text
>id='MyInput'></INPUT>"
>...

>As well, I have the following defined:

>function MyInput_onchange()
>    msgbox "MyInput has been changed."
>end function

>Well, this never ever gets called.  I tried using 'onclick' and it too
>never gets called, but I can capture it with

>function document_onclick
>     if window.event.srcElement.id = "MyInput" then
>        ' do something
>    end if
>end function

>So I tried the same with document_onchange, but nope it doesn't work.

>Is there something I'm unaware of or is this a bug?  It seems like using
>innerHTML doesn't "link-into" the
>DOM so events don't seem to be handled...

>I really need a solution/workaround to this as I build a list using
>VBScript and DOM but still need to react
>to events occuring in list entries.

>Thank you,

>Mark Gabriel



Tue, 15 May 2001 03:00:00 GMT  
 BUG??? or am I doing something wrong...

Quote:

> Let us know what happens, or if you find another solution.

Well one solution I have found is that since the elements are added dynamically
I cannot use the standard VBScript event handling.  But if you use JScript (with
the FOR and EVENT parameters) it will capture the events.  Although this means
some mixed scripting it works and is the route I will take.  Apparently the new
VBScript (i.e. IE5) supports dynamically added elements.

Thank you for your reply!

Mark Gabriel



Wed, 23 May 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. BUG??? or am I doing something wrong...

2. Not running, am I doing something wrong

3. Am I doing something wrong ?

4. Am I doing something wrong...??

5. Am I doing something wrong...??

6. onkeypress - what am i doing wrong

7. newbie - what am I doing wrong?

8. What am I doing wrong?

9. What I am doing wrong.

10. exitserver and settransfer, what am I doing wrong?

11. What am I doing wrong here?

12. What am i doing wrong here????? HELP

 

 
Powered by phpBB® Forum Software