Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

event.target on Firefox

 var x = event.target||event.srcElement;
 document.getElementById(x.id).style.left =  200 + "px" ;
 document.getElementById(x.id).style.top  =  100 + "px" ;

Works fine on Google Chrome and IE but not on Firefox. Tried it on Google. Google says event.srcElement (works on IE but not on Firefox) so I have added event.target but still not working. Is there anymore changes I need to do to work on Firefox? By the way I'm using 3.5 version of Firefox.

   function up()
       {
            dragok = false;
            document.onmousemove = null;
            var x = event.target||event.srcElement;
            document.getElementById(x.id).style.left= 200 + "px" ;
            document.getElementById(x.id).style.top= 100 + "px" ;
       } 

Please help me to make it work on Firefox

like image 695
niko Avatar asked Sep 17 '11 19:09

niko


People also ask

Is event target deprecated?

target are obsolete, not that Event. target itself is obsolete. The DOM (Living Standard) specification is not marked as obsolete and you should use that definition.

What is the use of event target?

Definition and Usage The target event property returns the element that triggered the event. The target property gets the element on which the event originally occurred, opposed to the currentTarget property, which always refers to the element whose event listener triggered the event.

What is the difference between target and currentTarget?

target is the root element that raised the event. currentTarget is the element handling the event.

Why is event target undefined?

You're likely getting this error because you're trying to get a name attribute on elements that don't have a name attribute. For example; input, textarea, and form elements are naturally able to get a name attribute. But elements like div, span doesn't.


2 Answers

Make sure you define event as a formal parameter to the handler.

IE defines it globally, and Chrome defines it both in both places, so it works either way, but Firefox only defines it as a function parameter.

function up( e ) {
    //       ^-----------------------------------------------------+
    if( !e ) e = window.event; // <---needed this --- and this ->--+

    dragok = false;
    document.onmousemove = null;
    var x = e.target||e.srcElement; // <--- and these
    document.getElementById(x.id).style.left= 200 + "px" ;
    document.getElementById(x.id).style.top= 100 + "px" ;
} 
like image 195
user113716 Avatar answered Sep 28 '22 05:09

user113716


I solved my problem using Jquery. For example to get the id of a element you can use:

var x = $(this).attr('id');
like image 45
LipeTuga Avatar answered Sep 28 '22 04:09

LipeTuga