tags:

views:

48

answers:

2

I have a series of links, each with their own id:

<li><a id="about" href="#">About</a></li>
<li><a id="call-us" href="#">Contact us</a></li>
<li><a id="home" href="#">Head on home</a></li>

Instead of writing a series of statements like this:

$(document).ready(function(){
    $("a.about").click(function(event){
        $("#content").load("/data.html #about");
    });
    $("a.call-us").click(function(event){
        $("#content").load("/data.html #call-us");
    });
});

Is there a way to abstract out the fact that when I click on a link with the class "ajax", I take its #ID, look at .data.html, and pull from data.html the div with the id #ID?

(I understand right now this doesn't specify the class, but let's just say I have 5 anchors like

<a href="#" class="ajax" id="UNIQUE-ID">Anchor Text</a>

It'd certainly make for code that was easier to maintain. Any input would be grand.

+2  A: 

Yes, quite easily:

$(document).ready(function(){
    $("a.ajax").click(function(e){
        $("#content").load("/data.html #" + this.id);
        e.preventDefault();
    });
});
Doug Neiner
Cool! What's the function(e) bit doing?
Alex Mcp
Alex, `e` is the "Event" object that is passed to the function. This let the original behavior be redefined.
The Wicked Flea
If you click an `a` tag with a `href='#'` without canceling the default behavior, the page will often scroll to the top or bottom and append `#` to the url. You had `function(event)` which is the same as `function(e)`, but the important part is the `e.preventDefault()` or `event.preventDefault()` which keeps the browser from screwing up :)
Doug Neiner
A: 

You could give each link a class like this:

<li><a id="about" href="#" class="ajax">About</a></li>
<li><a id="call-us" href="#" class="ajax">Contact us</a></li>
<li><a id="home" href="#" class="ajax">Head on home</a></li>

but assuming all links in the list need this click handler, I would be inclined to instead use a more minimalist approach and give the list itself an ID:

<ul id="links">
  <li><a id="about" href="#">About</a></li>
  <li><a id="call-us" href="#">Contact us</a></li>
  <li><a id="home" href="#">Head on home</a></li>
</ul>

and then

$(function() {
  $("#links a").click(function() {
    $("#content").load("/data.html?id=" + this.id);
    return false;
  });
});

As for your call to "/data.html id" I'm not sure what "data.html" is exactly but generally you want to pass the ID as a GET or POST parameter as I've done above but what you need to do here depends on what exactly you're calling.

cletus
I understood it to mean he was using jQuery's ability to filter the result and only use a part. So `data.html #about` would use only the contents of an element with `id="about"` on the `data.html` page.
Doug Neiner
data.html is just another page that's holding my rotating, static content.
Alex Mcp
@dcneiner, that's correct. I actually have 10 product icons on a page, and when you click on one, the description (in data.html, in <div id="product-title">) will get loaded into the "landing area" div
Alex Mcp