Jump to content

Event Return False Contines To "bubble"


GerryH
 Share

Recommended Posts

Greetings again!I'm having trouble cancelling an onclick event from executing in javascript when I try to pass parameters to the onclick function. What I'm attempting to accomplish is to have an <a> perform some AJAX functions if the user has javascript enabled, otherwise process the <a> normally. If I don't pass any parameters, this block works fine;

<script...>function changePage(){   alert("Changing Page");   return false;}document.getElementById('p1').onclick = changePage;</script><html...><ul id="nav_menu"><li id="menu1"><a id="p1" href="index.php?p=p1"></a></li>...</html>

However if I want to pass a parameter to my changePage function;

<script...>function changePage(pageNum){   alert(pageNum);   return false;}document.getElementById('p1').onclick = function(){changePage('p1')};</script><html...><ul id="nav_menu"><li id="menu1"><a id="p1" href="index.php?p=p1"></a></li>...</html>

The alert window displays, but then continues to redirect the page to the link. Can someone point out what I'm missing?Thanks

Edited by Gerry H
Link to comment
Share on other sites

first of all, you have to wait for the element to finish loading, before assigning the function to it, otherwise you will get an null or undefined error. use the onload event, to wait for the page to be fully loaded, before assigning a function to the required element.next assign the return false; with the function.see below:<script type="text/javascript">/*<![CDATA[*//*---->*/function changePage(pageNum){ alert(pageNum); //return false;}function initiate(){document.getElementById('p1').onclick = function(){changePage('p1');return false;};}window.onload=initiate;/*--*//*]]>*/</script>

Link to comment
Share on other sites

Thank you so much for the quick reply!Modifying the assignment;

document.getElementById('p1').onclick = function(){loadPage('p1');return false;};

works perfectly! However, in my first example where there were no params to the function, the function returned the value I set e.g. "return true" continued to load the link, that's what is confusing me.And I'm sorry for my bad code snippet examples, the above code is called in an initialization function in the window.onload event.Thanks again for quick response!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...