7

This works in Firefox, but not IE. Any help would be much appreciated! Thanks!

  var form = document.getElementById('theform')
    /* create the event handler */
    form.gen.onclick = function( evt ) {
        var f = evt.target.form
        var y = f.year.value
        var m = f.month.value
        genCalendar( document, y, m, 'theCalendar' )
    }
  • 6
    you should end statements with `;` - the implicit `;` at line endings is a fallback mechanism and I'd consider using it bad practice (other's may disagree) – Christoph Jun 06 '09 at 20:41

5 Answers5

15

To get the target of an event in both standards compliant browsers and IE, use

var target = evt ? evt.target : window.event.srcElement;

There's an overview of the different properties of event objects at MDC.

Christoph
  • 149,808
  • 36
  • 172
  • 230
1

As mentioned, IE does not pass the event object as a parameter. Try this:

var form = document.getElementById('theform')
  /* create the event handler */
  form.gen.onclick = function( evt ) {
    if(!evt)
      evt = window.event;
    var f = evt.target.form
    var y = f.year.value
    var m = f.month.value
    genCalendar( document, y, m, 'theCalendar' )
}

Or better yet, use a cross-browser library, like Prototype.js or jQuery.

gnud
  • 73,015
  • 5
  • 56
  • 76
1
  • When does this script run? You might have to run this script onload, after the DOM is fully loaded
<script>

function go()
{
  alert('dom is loaded:  register event handlers now') ;
}

</script>

<body onload=" go(); ">



</body>

bobobobo
  • 57,855
  • 58
  • 238
  • 337
0

From my own searching the most sucessful was this

function clickHandler(e){
 var elem, evt = e ? e:event;
 if (evt.srcElement)  elem = evt.srcElement;
 else if (evt.target) elem = evt.target;

 alert (''
  +'You clicked the following HTML element: \n <'
  +elem.tagName.toUpperCase()
  +'>'
 )
 return true;
}

document.onclick=clickHandler;

Sourced from ther very helpful and explanatory http://www.javascripter.net/faq/eventtargetsrcelement.htm

Jetblackstar
  • 239
  • 2
  • 10
0

This is why you should consider using a javascript library such as jquery, YUI, or prototype. These libraries abstract away the browser based differences which simplifies your coding.

Glenn
  • 7,352
  • 3
  • 26
  • 38