0

relatively new jQuery user(used it in the pass but really trying to understand it now).

Am trying to create my own jQuery pagination with ajax, so far my code looks like so:

$(document).ready(function(){
    $('.pigs ul.pigspagination li a').live('click', function(e) {
        e.preventDefault();
        var url = $(this).attr('href');
        if(url.match('^http')){
            return true;
        } else {
            var element = $(this).closest(".pigs");
            $(element).append('<div id="loading"><img alt="Loading..." src="loading.gif" /></div>');
            $('#loading').fadeIn('normal');
            $(element).load(url + ' ' + element);
            return false;
        }
    });
});

The code basically says on click of ".pigs ul.pigspagination li a" load the parent element which is ".pigs" from the the given url then load the same fragment from the url. However this does not seem to be working:

$(element).load(url + ' ' + element);

I assume that am loading the url then am adding a space then saying for the element which is referenced from var element = $(this).closest(".pigs");

if your wondering why i just cant put $(element).load(url + ' .pigs');

It is because i reference .pigs multiple times and its automatic since it the php gallery script i wrote.

The html looks roughly like this:

<div class="pigs">
    <ul class="pigs-gallery">
        <li><a href="the link" target="_blank"></a></li>
        ...
    </ul>
    <ul class="pigspagination">
        <li><a href="pagination link">&lt; Prev</a></li>
        ...
        <li><a href="pagination link">Next &gt;</a></li>
    </ul>
</div>

times however many more galleries are loaded.

Any ideas?

--------------EDIT for bfavaretto-------------- Tried this

var frag = $(this).closest(".pigs").attr('id');
$(element).load(url + ' #' + frag);

it loads just that suction but removes all the code, my guess is frag is being passed badly. the .attr('id'); will get the id by its self, not the '#' right?

Finale code if anyone is interested:

$(document).ready(function(){
    $('.pigs ul.pigspagination li a').live('click', function(e) {
        e.preventDefault();
        var url = $(this).attr('href');
        if(url.match('^http')){
            return true;
        } else {
            var element = $(this).closest(".pigs");
            $(element).append('<div id="loading"><img alt="Loading..." src="loading.gif" /></div>');
            $('#loading').fadeIn('normal');
            var frag = $(this).closest(".pigs").attr('id');
            $(element).load(url + ' #' + frag);
            return false;
        }
    });
});
FireDart
  • 3
  • 2
  • The first argument to `$.load` must be a valid URL. I don't understand what you are trying to do. Do you need to filter the HTML returned by the ajax call before inserting it into `$(element)`? – bfavaretto Jul 28 '11 at 16:45
  • @bfavaretto $(element) is selecting the parent of the clicked linked, which is .pigs. So of the linked click find the closest .pigs which is the parent. Hopefully that made some sense. and the url is the link of the click linked. I know that works since i have used it before. – FireDart Jul 28 '11 at 17:06
  • Your url gives the next page of results, right? Have you tried using simply `$(element).load(url)`? – bfavaretto Jul 28 '11 at 17:15
  • @bfavaretto i need the fragment of that page thought since i have multiple galleries on each page with the same class, so i was hoping with $(this) I can some how trick it into only giving me $(this) link's parent. Am i just trying to do something jquery cant do? – FireDart Jul 28 '11 at 17:20
  • Hey, wait, I noticed something else: you have `pagination link` as the href on your pagination links. These are not valid URLs, and cannot be used on ajax calls such as `$.load`. So, do you have a valid url returning the next page of your galleries? Where is it? – bfavaretto Jul 28 '11 at 17:48
  • Its a real url, i just put that in for filler text. An example of one of the url would be as followed: index.php?pigsgallery=1&pigspage=1 – FireDart Jul 28 '11 at 17:49

1 Answers1

0

After what we dicussed on the comments, I think I understand what you want.

Your example URL index.php?pigsgallery=1&pigspage=1 looks good. I assume it will only return the contents of pigsgallery X, on page Y. Perfect.

So your problem seems to be inserting the contents returned by the URL into the right gallery div-- the one which contains the pagination link clicked. You are almost there. As I suggested on one of the comments, $(element).load(url) should word. It means "load the url, and replace the contents of $(element) with the response".

Your HTML should look like the following (with both galleries currently on page 2):

<div class="pigs" id="pigs1">
    <ul class="pigs-gallery">
        <li><a href="the link" target="_blank"></a></li>
        ...
    </ul>
    <ul class="pigspagination">
        <li><a href="index.php?pigsgallery=1&pigspage=1">&lt; Prev</a></li>
        ...
        <li><a href="index.php?pigsgallery=1&pigspage=3">Next &gt;</a></li>
    </ul>
</div>

<div class="pigs" id="pigs2">
    <ul class="pigs-gallery">
        <li><a href="the link" target="_blank"></a></li>
        ...
    </ul>
    <ul class="pigspagination">
        <li><a href="index.php?pigsgallery=2&pigspage=1">&lt; Prev</a></li>
        ...
        <li><a href="index.php?pigsgallery=2&pigspage=3">Next &gt;</a></li>
    </ul>
</div>

If this is not how your HTML looks like, you may be confusing what should be done in PHP and what requires jQuery. PHP should handle the request and return just the content you need to replace on one of the divs. Then jQuery should be used to actually insert that content into the page, inside the right element (which you already have with var element = $(this).closest(".pigs").

bfavaretto
  • 71,580
  • 16
  • 111
  • 150
  • I see our confusion now, there are multiple galleries on one page and ?pigsgallery= only tells the script if the pagination is paging the right gallery. this may help [image](http://img121.imageshack.us/img121/3629/stackoverflowproblem.jpg) Thats why i need to tell jQuery to only get this links parent from the page. – FireDart Jul 28 '11 at 18:12
  • I see, i was trying to avoid adding more code to my php, make it as dynamic as possible but guess I can't. Need to add an id. just on finally thing. the user has the option to put in a custom id for the gallery, however i will have a default id generator from the folder name. So would be come
    . So can i use something like var element = $(this).attr('id'); to get the id for jquery? ----------EDIT--------- Edited my first post to add more code.
    – FireDart Jul 28 '11 at 18:38
  • Yes, but remember that you can't have anything inside an HTML id attribute (see [here](http://stackoverflow.com/questions/70579/what-are-valid-values-for-the-id-attribute-in-html)) – bfavaretto Jul 28 '11 at 18:44
  • am stupid, am telling jQuery to get $(this) yet it has not idea what am trying to get! so $(this).closest(".pigs") would work! Thank you so much. Not exactly what i wanted to do but i do understand how jQuery works a little better now. +1 – FireDart Jul 28 '11 at 18:49