views:

42

answers:

1

Hey guys,

I'm trying to push the coordinates of a mouse click on a particular element to the end of an array. This is what I have:

IN THE HEAD:

var seatsArray = [];

IN THE BODY:

var coordinates = document.getElementById("image");
coordinates.onclick = function(event) {
  seatsArray.push(offsetX, offsetY);
}
document.write("Seats array contains: " + seatsArray + ".");

Probably something simple, but I'm new to javascript, so I'd appreciate any help as to why it isn't working!

Thanks

+1  A: 

Two things I see, alter your on click event to this:

coordinates.onclick = function(event) {
  event = event || window.event;
  if (!window.event) { // thanks firefox
    event.offsetX = event.layerX;
        event.offsetY = event.layerY;
  } 
  seatsArray.push(event.offsetX, event.offsetY);
}

Firefox offsetX/Y is layerX/Y, and ie have a global event object.

But if you wnt global cordinates try this:

coordinates.onclick = function(e) {
e = e || window.event;

if (e && e.pageX && e.pageY) {
            e.posX = e.pageX;
            e.posY = e.pageY;

        } else if (e && e.clientX && e.clientY) {
            var scr     = {x:0,y:0},
                object  = e.srcElement || e.target;
            //legendary get scrolled
            for (;object.parentNode;object = object.parentNode) {
                scr['x'] += object.scrollLeft;
                scr['y'] += object.scrollTop;
            } 
            e.posX = e.clientX + scr.x;
            e.posY = e.clientY + scr.y;
        } 
 seatsArray.push(e.posX, e.posY);
}
Roki
Ah yes. Still seems not to be pushing to the array as the document.write outputs only the text I specified and no coordinates when I click the image.Thanks
IceDragon
Global script looks perfect, however, still no array output from document.write. Might it be to do with the fact that the "image" element is zoomable and panable (<img onLoad="shiftzoom.add(this);" id="image".......>)?Thanks
IceDragon
@IceDragon: You're issuing `document.write` calls in your `click` handler?
Roatin Marth
RM, The document.write is not in the click handler.
IceDragon
@IceDragon: ok so you're issuing the `document.write` call after you've *attached* the click handler. The array is empty at that point. You have to actually physically click on stuff to get the array populated.
Roatin Marth
Try don't use document.write, print positions with a method, it's more w3c frendly:function printPositions(el) { var result = "Seats array contains: "; for (var i = 0, len = seatsArray.length; i < len; i+=2) { result += seatsArray[i] +' , '+ seatsArray[i+1] + '<br/>'; } seatDiv = document.createElement('div'); seatDiv.innerHTML = result; el.appendChild(seatDiv);}
Roki
omg how stupid of me, document.write can't write text after the page is loaded. Do you know how I can get the text to change to display the array AS i click the "image" element, without reloading the page? I don't actually need to implement this in my final script, I just want to see that the array is working and see how its output changes as I edit it. Thanks
IceDragon
@IceDragon: change the `document.write` to `alert`.
Roatin Marth
That doesn't work because the alert just comes up when the document loads, and doesn't display again. Thanks
IceDragon
@IceDragon: **move your debugging code into the code you're trying to debug**. In other words, alert *inside* the click handler.
Roatin Marth