Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-09-2003, 01:10 PM   PM User | #1
Ulgen
New Coder

 
Join Date: Jul 2003
Location: Norway
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Ulgen is an unknown quantity at this point
Dynamically adding onmouseover function with argument

I've searched the forums on this, and what I found didn't work for me.

I want to add an onmouseover function to an element on the page. Problem is that I need to send that function an argument. I tried the following:

element.onmouseover = new Function('alert(this.text)')

which someone else said that worked, but it only displays 'undefined'. And yes, I've defined what I try to alert; if I call the same alert on the line before it displays correctly.

What can I do?
Ulgen is offline   Reply With Quote
Old 07-09-2003, 06:31 PM   PM User | #2
jkd
Senior Coder

 
jkd's Avatar
 
Join Date: May 2002
Location: metro DC
Posts: 3,163
Thanks: 1
Thanked 18 Times in 18 Posts
jkd will become famous soon enough
element.onmouseover = function() {
alert(element.text);
}

should do it. You're guarenteed to have the variable "element" in scope.

Although:

element.onmouseover = function() {
alert(this.text);
}

should work equally as well, as in that situation "this" refers to element.
__________________
jasonkarldavis.com
jkd is offline   Reply With Quote
Old 07-10-2003, 07:20 AM   PM User | #3
Ulgen
New Coder

 
Join Date: Jul 2003
Location: Norway
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Ulgen is an unknown quantity at this point
Hey, thanks. I finally figured it out.

The problem was that I wrote like this:

this.elemId = "someId"
this.docElem = document.createElement("div")
this.docElem.id = this.elemId

this.docElem.onmouseover = function() {
alert(this.elemId)
}


which didn't work. (Note: I do append the element, just not here)

But this worked:

this.docElem.onmouseover = function() {
alert(this.id)
}

So this worked out in my particular situation. But what if I had a:

this.text = "Wheee"

How could I reach that within the function? By doing:

this.docElem.text = this.text ??
Ulgen is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:55 AM.


Advertisement
Log in to turn off these ads.