3

In Firefox 6 I tried to get the target element on which the event occurred, but it does not show any element and it shows undefined in alert. Tried to debug it using the Firebug tool and found the attribute "target" missing for the event object. Can anyone help me out? I do have the code below:

function getSource(event)
{
    if(!event) 
    { 
        field = window.event.srcElement;
       alert(field);
    }
    else
    {
        field = event.target; 
        alert(field) //Getting undefined in FF6
    }
}

Edited Portion:

document.onkeypress = getSource;
document.onmouseup = getSource;

Any help would be appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
AmGates
  • 2,127
  • 16
  • 29
  • what **type** of event is this (and what's the `target` you are expecting)? Does it work on all other browsers? – Saket Sep 27 '11 at 06:39
  • It is a click event the target can be either, button, textbox, etc – AmGates Sep 27 '11 at 06:41
  • can you show the HTML snippet where the handler `getSource()` is registered? Or perhaps, the entire HTML is that's possible. – Saket Sep 27 '11 at 06:43
  • Without any more information or code, I'm going to have to say this probably isn't a problem in FF6. See https://developer.mozilla.org/en/DOM/event.target , https://developer.mozilla.org/en/DOM/event/Comparison_of_Event_Targets , http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-interface – Rusty Fausak Sep 27 '11 at 06:43
  • See the edited portion. @rfausak: I tried the same code in firefox 3 and am getting what I want, but only in firefox6 I get this problem. – AmGates Sep 27 '11 at 06:46
  • possible duplicate of [event.target not working on Firefox](http://stackoverflow.com/questions/7457260/event-target-not-working-on-firefox) – Shadow The GPT Wizard Sep 27 '11 at 06:51

4 Answers4

3

Try the code below

function getSource(e)
{
     if(!e)
        e = window.event;
     field = evt.srcElement || evt.target;
     alert(field);
     return true;
 } 

Hope this helps you.

ahamed
  • 730
  • 1
  • 5
  • 8
1

Test this in Fx 6:

<script type="text/javascript">

window.onload = function() {
  document.getElementById('d0').onclick = showTarget;
}

function showTarget(e) {
  e = e || window.event;
  var target = e.target || e.srcElement;
  alert(target.tagName);
}

</script>

<div id="d0">
  <p>click on me</p>
</div>

It should alert "P".

RobG
  • 142,382
  • 31
  • 172
  • 209
0

As also explained in the similar question, change the function to this:

function getSource(evt)
{
    if(!evt) 
        evt = window.event;
    if (evt) {
        field = evt.srcElement || evt.target;
        alert(field);
        return true;
    }
    alert("event not found");
    return false;
}
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
0
function getSource(ev) {
  var el=(ev=ev||window.event).target||ev.srcElement;
  alert(el+" "+el.tagName);
}
Andrew D.
  • 8,130
  • 3
  • 21
  • 23