object.onmouseover

Discussion in 'Javascript' started by Laser Lips, May 21, 2008.

  1. Laser Lips

    Laser Lips Guest

    HI All,

    Is it possible to do the following?

    object = document.getelementById('something');

    object.onmouseover='alert("hi");';

    ...................


    I know that object.onmouseover=function(){alert("hi");} is OK but I
    want to do it the first way. Can it be done?

    Graham
    Laser Lips, May 21, 2008
    #1
    1. Advertising

  2. Laser Lips wrote:

    > object.onmouseover='alert("hi");';
    >
    > ..................
    >
    >
    > I know that object.onmouseover=function(){alert("hi");} is OK but I
    > want to do it the first way. Can it be done?


    Assigning a function makes sense, assigning a string as you do does not
    make sense. So it can be done but is not in any way useful. If you have
    a string then you need to create a function from it:
    object.onmouseover = new Function('alert("hi");');


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, May 21, 2008
    #2
    1. Advertising

  3. Laser Lips

    Laser Lips Guest

    Thats just what I needed thanks. :0)
    Graham
    Laser Lips, May 21, 2008
    #3
  4. Laser Lips escribió:
    > Is it possible to do the following?
    >
    > object = document.getelementById('something');
    >
    > object.onmouseover='alert("hi");';

    [...]
    > I know that object.onmouseover=function(){alert("hi");} is OK but I
    > want to do it the first way. Can it be done?


    Do you need to execute code from a string or is it just an aesthetics
    question?

    In the latter case, this is not HTML; you don't need to pack all your
    code in one line. So (in my humble opinion) this looks just fine:

    object.onmouseover = function(){
    alert("hi");
    };

    You can also assign a named function:

    function sayHi(){
    alert("hi");
    }

    object.onmouseover = sayHi;


    In the former case you'd have to do this:

    object.onmouseover = function(){
    eval('alert("hi");');
    };

    Not a good idea unless there's powerful reason.


    --
    -- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
    -- Mi sitio sobre programación web: http://bits.demogracia.com
    -- Mi web de humor al baño María: http://www.demogracia.com
    --
    Álvaro G. Vicario, May 21, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tor Inge Rislaa
    Replies:
    3
    Views:
    16,124
    Jerry III
    Aug 6, 2003
  2. Jay
    Replies:
    2
    Views:
    41,668
    Alex Papadimoulis
    Sep 30, 2003
  3. Frances Valdes

    Onmouseover

    Frances Valdes, Mar 2, 2004, in forum: ASP .Net
    Replies:
    7
    Views:
    781
    Frances Valdes
    Mar 3, 2004
  4. =?Utf-8?B?QXZpIFNoaWxvbg==?=

    OnMouseOver for a DataGrid control

    =?Utf-8?B?QXZpIFNoaWxvbg==?=, Jul 12, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    2,174
    Mr. Dot Net
    Jul 12, 2004
  5. Ryan Moore

    DataList <td onMouseOver>

    Ryan Moore, Oct 11, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    3,290
    Eliyahu Goldin
    Oct 12, 2004
Loading...

Share This Page