I'm getting started with jquery and following the tutorial on the official website found here. http://docs.jquery.com/How_jQuery_Works#jQuery:_The_Basics
I'm in the section labeled Launching Code on Document Ready. If you notice, they provide two examples. One where an alert box pops up before taking you to jquery site, and another where an alert box prevents you from going to the site.
Suppose I want to have two links. One where an alert box appears and upon clicking "OK" it then proceeds on to jquery's site, and another that an alert box appears but prevents you from going to jquery's site. I'd just like to be able to figure out different responses for different links. Do I need to give it some sort of id?
Here's the code.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Demo</title>
</head>
<body>
<script src="jquery.js"></script><br/>
<a href="http://jquery.com/" id="1">jQuery</a><br/> <!--first link: will display message and then proceed to site -->
<script>
$(document).ready(function(){
$("a#1").click(function(event){
alert("Thanks for visiting!");
});
});
</script>
<a href="http://jquery.com/" id="2">jQuery</a> <!-- second link: message appears and does not continue to site -->
<script>
$(document).ready(function(){
$("a#2").click(function(event){
alert("As you can see, the link no longer took you to jquery.com");
event.preventDefault();
});
});
</script>
edit - added id's to anchors. Thank you guys, it works.
Yes, using id's is the most direct way to reference the anchors.
<a id="anchor1" href="..">anchor 1</a>
allows
$("#anchor1").click(function(event){
alert("Thanks for visiting!");
event.preventDefault();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With