Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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?

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    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.

  • #3
    New Coder
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 ??


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •