create event -property

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ciary
    Recognized Expert New Member
    • Apr 2009
    • 247

    #1

    create event -property

    hi all,

    i have another problem. in my website, i have an option to create divs dynamically. the problem is that i want to do something when you click on one of those divs. i thought this would do the trick
    Code:
    document.getElementById("MyDiv").onClick = "MyFunction()"
    unfortunatly, it doesn't. :(

    does anyone know how it should be done?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Try:
    Code:
    document.getElementById("MyDiv").onclick = MyFunction;

    Comment

    • Ciary
      Recognized Expert New Member
      • Apr 2009
      • 247

      #3
      ok, that works. but now i need to send an argument when i execute the function.

      Code:
      document.getElementById("MyDiv").onclick = MyFunction('foo');
      this doesn't seem to work since it triggers the event when i create it but not when i click on the div.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        You need to set it to a function object rather than a function call which is why I removed the parenthesis earlier. Try this:
        Code:
        document.getElementById("MyDiv").onclick = function() {MyFunction('foo');}

        Comment

        • Ciary
          Recognized Expert New Member
          • Apr 2009
          • 247

          #5
          acoder, you're the best. :)

          ty so much

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            You're welcome :) Glad to help.

            Comment

            Working...