Setting events dynamically!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • olakara
    New Member
    • Nov 2006
    • 18

    Setting events dynamically!

    hi everybody,
    i have a problem.. i get some html through ajax from a sevlet. before displaying it on screen i dynamically create a DIV element using createElement. I specify the CSS for this DIV as follows:
    Code:
    subMenu = document.createElement('<div>');
    subMenu.id = "subArea";
    I tried subMenu.onmouse over = fun; But its' not working!

    Now I want to give some Event to this div.. how do i do it?
    Thanks in advance.
    -- Abdel Raoof Olakara
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    It should work. I assume you have already defined fun as a function, but you also need to append this div to the document, e.g. to the document body:
    Code:
    document.body.appendChild(subMenu);

    Comment

    • olakara
      New Member
      • Nov 2006
      • 18

      #3
      Originally posted by acoder
      It should work. I assume you have already defined fun as a function, but you also need to append this div to the document, e.g. to the document body:
      Code:
      document.body.appendChild(subMenu);
      I have added the subMenu as you said to the document.. but still the function fun() won't work! If you use Firebug you will see that it is not attached to the DIV or any element that i am trying to put the event to.
      Thanks in advance
      -- Abdel Olakara

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        For it to work, you need to add some content to the div, e.g.
        Code:
        subMenu = document.createElement('<div>');
        subMenu.id = "subArea";
        subMenu.innerHTML = "subArea";
        subMenu.onmouseover = fun;
        document.body.appendChild(subMenu);

        Comment

        Working...