Can someone explain how to integrate stop propagation in javascript into my code? I had imagined there would be plenty of clear examples online, but the examples I've seen haven't helped me understand.
I keep seeing this code snippet as an explanation:
function doSomething(e)
{
if (!e) var e = window.event;
e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();
}
But i guess my javascript literacy is 0, because im having difficulty determining which of that is literal and which of that is a place holder variable for something im supposed to enter differently.
Like, let's say i have an onclick for a list item and its parent element. And onclick it calls an ajax function, but i need to prevent it from bubbling up.
So is the above function supposed to stand alone? Or is that supposed to represent code that i add into my already existing function?
What is e and why am i passing it as a parameter in my function?
i dont understand what if(!e)
would entail. What is e, and when would the conditions for e not being true be met?
This is my code:
ajax query:
function serverconnect(divID, $clickvalue, datalocation)
{
var1=$clickvalue;
if(xmlhttp)
{
var obj = document.getElementById(divID);
xmlhttp.open("GET", datalocation);
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 &&
xmlhttp.status == 200)
{
obj.innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null);
}
alert(var1);
serverconnectPP('productpane6', 'clickit', 'ProductPane.php')
}
html:
<li onclick="serverconnect(var1, var2, var3)">Test
<ul>
<li onclick="serverconnect(var1, var2, var3)">Test</li>
</ul>
</li>
The html isnt too important but the point is if i click on the inner li, both onclicks trigger.
I understand jquery is fantastic but id like a javascript explanation please.