I’m writing a function which requires knowing the location of the clicked div.
I’m wondering if I can get the location of a clicked object as a javascript variable?
here is the code.
HTML
<area shape="rect" coords="103, 0, 213, 25" href="#" onClick="swap3($(this),'product-details','product-specs');">
Javascript:
function swap3(currentDivId ,oldDivId, newDivId) {
var oldDiv = currentDivId.nextAll("div." + oldDivId);
var newDiv = currentDivId.nextAll("div." + newDivId);
oldDiv.style.display = "none";
newDiv.style.display = "block";
}
$()returns a DOM element (like an object that you can work with it’s methods, properties, etc) and if you set a variable to it, the variable must work like an jQuery-Object correctly. But in my experience, some times that DO NOT! and I learn the best way is to get the variable by jQuery-selector ($). Your code is correct, but should be better if you apply these changes: