Shawn
Shawn

Reputation:

Javascript IE Event

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

  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' )
    }

Upvotes: 7

Views: 10692

Answers (5)

Jetblackstar
Jetblackstar

Reputation: 249

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

Upvotes: 0

bobobobo
bobobobo

Reputation: 67234

  • 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>

Upvotes: 1

Christoph
Christoph

Reputation: 169593

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.

Upvotes: 15

Glenn
Glenn

Reputation: 8032

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.

Upvotes: 0

gnud
gnud

Reputation: 78528

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.

Upvotes: 1

Related Questions