Last active
October 17, 2016 15:37
-
-
Save krisclarkdev/570f16c015063db2742fb413c4f429b4 to your computer and use it in GitHub Desktop.
Example showing how to get the X&Y from a mouse click and place a DIV at that location
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dynamicDivIDs = new Array(); | |
function random() { | |
return Math.floor(Math.random() * 10000) + 1; | |
} | |
function newDivID() { | |
var newIDPrefix = 'dynamicDiv'; | |
var newID = newIDPrefix + random(); | |
for(i=0; i<dynamicDivIDs.length;i++) { | |
if(dynamicDivIDs[i] == newID) { | |
i=0; | |
newID = newIDPrefix + random(); | |
} | |
} | |
dynamicDivIDs.push(newID); | |
return newID; | |
} | |
function createDiv(event) { | |
placeDiv(event.clientX, event.clientY); | |
} | |
function placeDiv(x_pos, y_pos) { | |
var newID = newDivID(); | |
var d = document.createElement('div'); | |
d.id = newID; | |
d.style.position = "absolute"; | |
d.style.left = x_pos+'px'; | |
d.style.top = y_pos+'px'; | |
d.innerHTML = "ID = " + newID; | |
document.getElementsByTagName('body')[0].appendChild(d); | |
} | |
document.addEventListener("click", createDiv); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment