Reputation: 97
I am trying to get X/Y coordinates from a div with a mouseclick.
Now I want to collect more X/Y coordinates, not just one. so if I click in the div twice or more times, I want to list the coordinates under the div.
<div>"click"
"click"</div>
------------------------
coordinates 1: X/Y
coordinates 2: X/Y
.
.
.
Does anyone know how I do that?
Upvotes: 1
Views: 3715
Reputation: 2417
Here's exactly what you asked for.
Fiddle: http://jsfiddle.net/ZZEk8/118/
Add to HTML:
<span class="log"></span>
JS:
var clicks = [],
updatedClicks = "";
$('.clickable').on('click', function (ev) { //We don't use .bind() after jQuery 1.7, use .on() now.
var $div = $(ev.target);
var $display = $div.find('.display');
var offset = $div.offset();
var x = ev.clientX - offset.left;
var y = ev.clientY - offset.top;
$display.text('x: ' + x + ', y: ' + y);
clicks.push(x + "/" + y);
updatedClicks += "coordinates" + " " + clicks.length + ":" + " " + clicks[clicks.length -1] + "<br />";
$('.log').html(updatedClicks);
});
UPDATE: OP requested a way to limit the coords and delete one.
Fiddle: http://jsfiddle.net/ZZEk8/125/
var clicks = [],
updatedClicks = [],
limit = 5;
$('.clickable').on('click', function (ev) {
var $div = $(ev.target);
var $display = $div.find('.display');
var offset = $div.offset();
var x = ev.clientX - offset.left;
var y = ev.clientY - offset.top;
$display.text('x: ' + x + ', y: ' + y);
//Stops adding at limit
if (clicks.length < limit){
addCoord(x,y);
}
});
$('.delete').on('change', function(ev) {
if(clicks.length){
var selection = this.value -1;
deleteCoord(selection);
} else { //If there are no coords to delete run this
return false;
}
});
function addCoord (x,y){
clicks.push(x + "/" + y);
updatedClicks.push("Coordinates" + ":" + " " + clicks[clicks.length -1] + "<br />");
$('.log').html(updatedClicks.join(" "));
}
function deleteCoord(selection) {
clicks.splice(selection, 1);
updatedClicks.splice(selection, 1);
$('.log').html(updatedClicks.join(" "));
}
Upvotes: 2
Reputation: 196
If you want to save coordinates, you can collect them in an array, like this:
//Declare an array with 0 length
var arr = new Array(0);
$('.clickable').bind('click', function (ev) {
var $div = $(ev.target);
var $display = $div.find('.display');
var offset = $div.offset();
var x = ev.clientX - offset.left;
var y = ev.clientY - offset.top;
$display.text('x: ' + x + ', y: ' + y);
//increase the length of array before insert the value of coords
arr.length = arr.length+1;
//insert the value
arr[arr.length-1] = "coordinates" + arr.length + ":" + x +"/" + y;
});
Then, to operate coordinates you can tour your array with for-loop
Upvotes: 1
Reputation: 6873
I do it using div
elements, but you can change it.
See fiddle
My JS change is
$display.append($('<div />').text('x: ' + x + ', y: ' + y));
And in HTML
<div class='clickable'>
<div class='display'></div>
</div>
Or this one, with a srollbar when needed.
Upvotes: 1
Reputation: 21495
Add a new element <div id="log"></div>
after your existing element.
In your JavaScript code add
document.getElementById("log").innerHTML += "<br/>Coordinates: X=" + x + "; Y=" + y;
In your jsfiddle example:
$display.html($display.html() + '<br/> x: ' + x + ', y: ' + y);
Upvotes: 2