1

HTML:

<div class="container-fluid">
<div class="sidebar">
    <ul class="pills">  
        <li id="l1"><a id="link1">Lesson 1</a></li> <hr>
        <li id="l2"><a href="#" >Lesson 2</a></li> <hr>
        <li id="l3"><a href="#" >Lesson 3</a></li> <hr> 
    </ul>
   <div class="span16" id="target">
</div>

Javascript:

$('#l1').click(function(){
    $('#target').fadeOut('fast', function(){
        $('#target').load("lesson/lesson1.html", function(){
                $('#target').fadeIn('slow');

                });
            });

        });

I have 5 links within my webpage, I was wondering if there was anyway to make this one piece of code instead of copy + pasting it multiple times.

3
  • Show us the HTML, without it it would be guessing what you are trying to do. Commented Jan 30, 2012 at 4:27
  • usually for targeting groups you don't want to use IDs but rather classes or elements. Can you post your HTML? Commented Jan 30, 2012 at 4:28
  • 1
    Why in the world would you wait for the animation to finish before sending the AJAX request? Commented Jan 30, 2012 at 4:28

3 Answers 3

5
$('a.AjaxLink').click(function(){
    var url = this.href;

    $('#target').fadeOut('fast')
                .load(url, function(){ $(this).stop(true, false).fadeIn('slow'); });
    });

    return false;
});

This code handles the click event for all <a>s with a class of AjaxLink.

In the click handler, it grabs the href, fades out your #target, and performs the AJAX load.

When the AJAX load finishes, it stops the animation (in case the AJAX was faster than the fade), then fades it back in.

Finally, it tells the browser not to take the default action (navigating to the page) by returning false.

Sign up to request clarification or add additional context in comments.

8 Comments

Alright...could please explain the code? I'm still quite new to javascript.
@tsand: What don't you understand?
@tsand I think you need to put class="AjaxLink" to every link you want that to happen. Then the function should work for every <a> that have the class AjaxLink
@SLaks this.href and a.AjaxLink, I don't quite understand what's going on.
@tsand: this is a raw DOM element. It has an href property that returns value of the href="" HTML attribute. a.AjaxLink is a jQuery (or CSS) selector. api.jquery.com/category/selectors
|
0

Use class instead of id. Select elements using class.
Also you can use .each() method

Comments

0

You could do this with a new jQuery method. Given this HTML:

<a class="hello" href="#">Hello</a>
<a class="goodbye" href="#">Goodbye</a>

<div id="target"></div>

You'd use this code:

jQuery.fn.switchTarget = function( target, href ) {
    var $target = $(target);

    this.bind( 'click', function(e) {
        e.preventDefault();

        $target.fadeOut('fast', function() {
            $target.load( href, function() {
                $target.fadeIn('slow');
            });
        });
    });

    return this;
};

$('.hello').switchTarget( '#target', 'hello.html' );
$('.goodbye').switchTarget( '#target', 'goodbye.html' );

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.