Example is based on this answer https://stackoverflow.com/a/7132830/2465936
Here is working example http://jsfiddle.net/rigaconnect/aGWGn/3/
If click on <p id ="2310">click here to set variable to 2310</p>
then in <div id="load"></div> displays 2310.
Or myVariable=targ.id; becomes id= of clicked element.
Here is code
function whichElement(e) {
var targ;
if (!e) {
var e=window.event;
}
if (e.target) {
targ=e.target;
}
else if (e.srcElement) {
targ=e.srcElement;
}
if (targ.nodeType==3) {// defeat Safari bug
targ = targ.parentNode;
}
myVariable=targ.id;
document.getElementById("load").innerHTML=myVariable;
}
<body id="-1" onmousedown="whichElement(event)">
<p id="3.14">click here to set variable to 3.14</p>
<h3 id="5">click here to set variable to 5</h3>
<p id ="2">click here to set variable to 2</p>
<p id ="2310">click here to set variable to 2310</p>
<div id="load"></div>
Need to get following behavior:
If click on <p id ="2310">click here to set variable to 2310</p>
then in <div id="load"></div> displays click here to set variable to 2310.
Or myVariable=targ.id; value is clicked text.
As understand need something like this
``var myVariable = $("#???clicked id????").text();`
But do not understand how to correctly implement it in code
Please advice