[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1040
  • Last Modified:

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?
0
higginsonline
Asked:
higginsonline
  • 6
  • 6
  • 3
3 Solutions
 
SRigneyCommented:
I think you want to wait until ondomcomplete to call the function the first time.
0
 
higginsonlineAuthor Commented:
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...
0
 
SRigneyCommented:
There should be on oncontect complete.   Or the ajax that you call will return and allow you to process additional scripts when it completes.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
sh0eCommented:
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

0
 
SRigneyCommented:
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

0
 
sh0eCommented:
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

0
 
higginsonlineAuthor Commented:
@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...
0
 
sh0eCommented:
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.
0
 
higginsonlineAuthor Commented:
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.
0
 
sh0eCommented:
Would it be possible to see a demo? Or more of the context this is being used in?
0
 
higginsonlineAuthor Commented:
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.  
0
 
higginsonlineAuthor Commented:
That help at all?
0
 
sh0eCommented:
Sorry, I have been busy lately so I can't respond as quickly.  livequery won't work with objects that you don't create using jQuery (you are using Microsoft AJAX).  
0
 
higginsonlineAuthor Commented:
@sh0e

Np, Ahh that makes sense.. Guess I could have told you I was using Microsoft AJAX for part of it..
When the event is called I had to run this in my VB.Net code:
ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(String), "jquery", "$(function() { $('a').click(function(event) { event.preventDefault();var aSrc = $(this).attr('href');$('#displayPic').fadeOut('slow', function() { $('#displayPic').html('<img src=' + aSrc + ' />').fadeIn('slow'); });   });});", True)
0
 
sh0eCommented:
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?
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now