tags:

views:

35

answers:

1

Hi all,

I'm trying to achieve two things, both of which I fail to get at. On a server are a series of notes; all contain a div (id=ajxContent). Some notes also contain an additiona div (id=ajxHead). The ajxContent itself contains links (class=clicking) to call the next installment, hence the link destination in a variable.

This is my code:

$(document).ready(function(){
$( 'a.clicking' ).live('click', function(e){
    e.preventDefault();
    var theLink = $(this).attr('href');
    $('#loadText').load(theLink + '#ajxContent');
    $('h1').load(theLink + '#ajxHead');
});
})

Where it fails:

1) the ajxContent gets loaded into the h1 tag as well as in its intended target. 2) when I had set up the code differently, if there was no div id=ajxHead present in a note, the load would nevertheless erase the existing value in the h1 tag.

Am I on a totally wrong track here, with two load statements? Input is very welcome, thanks.

A: 

You need a space in there to get the a portion of the content, see the .load() docs for details:

$(document).ready(function(){
  $( 'a.clicking' ).live('click', function(e){
    e.preventDefault();
    var theLink = $(this).attr('href');
    $('#loadText').load(theLink + ' #ajxContent');
    $('h1').load(theLink + ' #ajxHead');
  });
});

Updated - Fix for your specific page:

$( 'a.clicking' ).live('click', function(e){
  e.preventDefault();
  $.get($(this).attr('href'), function(resp) {
       $('#loadText').html($(resp).filter('#ajxContent').html()); 
       var head = $(resp).filter('#ajxHead');
       if(head.length) $('h1').html(head.html());
  });
});

Because your elements are all root elements, which I just haven't come across, $(selector, context) doesn't work, since it does this internally: context.find(selector) In your case they aren't children of a root response element, they're all siblings at the root, do alert($(resp).length) to see this.

Anyway, the above code will do what you're after, tested here in Chrome and Firefox, looks good and only requesting the page once.

Nick Craver
Many thanks Nick. Your first installment resolved my first question, but the h1 still gets replaced when a note does not have an ajxHead div. Sadly your second option returns an all white screen?
Bussard
@Bussard - Updated, try the second option again.
Nick Craver
@Nick, nope, now the ajxContent doesn't load.
Bussard
@Bussard - Now neither work, or just the content?
Nick Craver
@Nick - Neither work, but previously the screen went all white; the values in both #loadText and h1 were deleted by the call. Now the initial h1 remains visible, but the content disappears and is not replaced by ajxContent.
Bussard
@Bussard - Try this: `$.get($(this).attr('href'), function(data, status) { alert(status); });` what do you get?
Nick Craver
@Nick: it loses formatting: just loading the note in an empty browser window. Here is a link to the working example, with your first option in the script: http://www.waldmark.com/tryout/pages/fadepage.htmlThanks for your patience.
Bussard
@Bussard - Have to run to dinner, will look at and fix right after :)
Nick Craver
@Nick - I removed a pair of closing marks }): and then the alert says: success.
Bussard
@Bussard - That was a tricky one, haven't come across the case before...updated the second block of code, leaving the first in case someone has the spacing problem later and finds this...let me know if the updated answer doesn't solve your issue.
Nick Craver
@Nick - You Hero! Works a charm, and content appears fast and crisp. Many thanks for sticking with me.
Bussard