Link to home
Start Free TrialLog in
Avatar of higginsonline
higginsonline

asked on

I want to prevent event Default for anchor links that are created after the page is loaded how is this possible?

In the header I have a jquery script that does:
    <script type="text/javascript">
        $(function() {
            $("a").click(function(event) {
                event.preventDefault();
            });
        });
    </script>

It should prevent the default event from clicking an anchor link, meaning I don't want it to actually open or goto a new page.  The problem is that the links are created after the page is loaded so that the script is never applied to them.  Is there a way to fix it? or re-apply the code without refreshing the page?
SOLUTION
Avatar of SRigney
SRigney
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of higginsonline
higginsonline

ASKER

My thing is, DOM and the rest of the page is completed awhile ago.  Then they click on a button and it goes out and retrieves information and populates it into a div, without refreshing the entire page.  That div contains links that I want to target with jquery...
There should be on oncontect complete.   Or the ajax that you call will return and allow you to process additional scripts when it completes.
You need the livequery plugin.
http://brandonaaron.net/docs/livequery/

<<script type="text/javascript" src="http://brandonaaron.net/docs/livequery/scripts/jquery.min.js"></script>
<script type="text/javascript" src="http://brandonaaron.net/docs/livequery/scripts/jquery.livequery.min.js"></script>
    <script type="text/javascript">
        $(function() {
            $("a").livequery("click", function(event) {
                event.preventDefault();
            });
            $(document.body).append('<a href="lorem">ipsum</a>');
            $(document.body).append('<a href="lorem1">ipsum1</a>');
        });
    </script>

Open in new window

Using Prototype I would use the following code.


When the call is complete in the setDefaultMethod I would then run your chunk of code.   It looks like jQuery lets you set a callback function similarly.

http://docs.jquery.com/Ajax/load#urldatacallback
var myAjax = new Ajax.Request(
  url, 
  {
    method: 'post', 
    parameters: params,
    onComplete: setDefaultMethod
  });

Open in new window

URLs were wrong.  Make sure you replace the script src locations with your local copies.  Working demo.
<script type="text/javascript" src="http://brandonaaron.net/docs/livequery/scripts/jquery.min.js"></script>
<script type="text/javascript" src="http://brandonaaron.net/jquery/plugins/livequery/jquery.livequery.js"></script>
    <script type="text/javascript">
        $(function() {
            $("a").livequery("click", function(event) {
                event.preventDefault();
            });
            $(document.body).append('<a href="lorem">ipsum</a>');
            $(document.body).append('<a href="lorem1">ipsum1</a>');
        });
    </script>

Open in new window

@sh0e:

The problem I am seeing is that it takes awhile for livequery to add the function to the tag.  If I click real quick on a link after the section loads, it will treat the url as a link and goto the page...
You can hide the section before it loads (or show a loading screen), bind click, then show the section.
It will depend on how you load the new section.

By asynchronous design, what you want to do will not have a trivial "automagic" solution.
Actually, it doesn't matter how long I give it to load sometimes (did a loader).  It seems like it is just a hit or miss control.  Sometimes the livequery works and sometimes it doesnt.
Would it be possible to see a demo? Or more of the context this is being used in?
Sure thing:

http://newcity.hol2.net/photos.aspx  click on the left picture to get a set, then click on the picture on the bottom right hand.  
That help at all?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Perhaps I need brushing up on my .NET, but won't that only work once?
If you load UpdatePanel1 more than once it will only execute the first time?