how can i tell jquery to use the <a id=""> or <a href=""> as a URL to load on click? For example URL page1.php. either by getting the href, or by getting the id and then appending .php
html
<a id="page1" class="load" href="page1.php">1</a>
<a id="page2" class="load" href="page2.php">2</a>
<a id="page3" class="load" href="page3.php">3</a>
jquery
<script>
$(document).ready(function () {
$(".load").click(loadDynamic);
});
function loadDynamic() {
$("#load_in")
.load("",function (content) {
$(this).hide().fadeIn("slow");
return false;
});
}
</script>
Use
this.hrefto get thehref.Of course you can use
this.id + ".php"as well.Note that you wrote
return falseinside theloadcallback thus it won’t prevent the default anchor click! You should move it to the outer scope like in the answer.For those of you complaining about
.click(fn)againston('click', fn)Those are the same function,
.click(fn)is an alias foron('click', fn):jQuery docs