18
 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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
niko
  • 9,285
  • 27
  • 84
  • 131
  • could you make a http://jsfiddle.net/ or http://jsbin.com/ example of the complete code which "does not work" ? – tereško Sep 17 '11 at 19:46
  • Could you please show more code (ideally a JSFiddle demo)? BTW, there's no need to look for `x`'s `id` and then look back for particular element by the `id`, you can just work with `x.style.left`. – duri Sep 17 '11 at 19:46

3 Answers3

46

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" ;
} 
user113716
  • 318,772
  • 63
  • 451
  • 440
1

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

var x = $(this).attr('id');
LipeTuga
  • 595
  • 5
  • 7
0

This solution to work in Firefox browser too. When start keydown action then its set the event property to global variable like this.

 var keyEvent=null; 

Assign that event property to keyEvent and .item is target element.

  $(document).on("keydown",'.item', function(e){ 

                keyEvent=e;                                    

        },this))

then.Use keyEvent in your code.

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

Note: Don't try to set the event property to global variable of window.

balaji s
  • 62
  • 4