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(even t) {
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?
<script type="text/javascript">
$(function() {
$("a").click(function(even
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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/
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>
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
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
});
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>
ASKER
@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...
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.
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.
ASKER
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?
ASKER
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.
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.
ASKER
That help at all?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
If you load UpdatePanel1 more than once it will only execute the first time?
ASKER