views:

626

answers:

2

I'm trying to turn "more" into a hyperlink, but it's like it totally ignores the last wrap... I'm sure that I'm doing it wrong, but not sure how... Help?

   $j('#sub > div[id^="post-"]').each(function() {
            var sid=this.id.match(/^post-([0-9]+)$/);
            var sfimg = $j(this).find("img");
            var sfhh = $j(this).find("h2");
            var sfpt = $j(this).find("p:not(:has(img)):eq(0)");
            var more = 'more';
            $j(this).html(sfimg);
            $j(sfimg).wrap($j('<a>').attr('href', '/blog/?p='+sid[1]));
            $j(this).append(sfhh).append(sfpt);
            $j(sfpt).wrap($j('<div>').attr('class', 'sfentry'));
            $j(this).append('<div class="morelink">'+more+'</div>');
            $j(more).wrap($j('<a>').attr('href', '/blog/?p='+sid[1]));
    });
A: 

Why do you expect $j(more) to match anything?

Jonathan Feinberg
A: 

You over-using the jquery function ($j(), in your case) and your doing things in the wrong order. Also, there may be cases (possibly) that $(this).find('img'), for instance, might return more than one element... Not sure of your scenario, though.

Try this (may not be perfect, but it should lean you in the right direction):

$j('#sub > div[id^="post-"]').each(function() {
    var sid   = this.id.match(/^post-([0-9]+)$/);
    var sfimg = $j(this).find("img");
    var sfhh  = $j(this).find("h2");
    var sfpt  = $j(this).find("p:not(:has(img)):eq(0)");
    var more  = 'more';
    sfimg.wrap($j('<a>').attr('href', '/blog/?p='+sid[1]));
    $j(this).html(sfimg);
    sfpt.wrap($j('<div>').attr('class', 'sfentry'));
    // You do realize what you have will append the paragraph to your h2 tag, right?
    // I think you want:
    /*
    $j(this).append(sfhh).end().append(sfpt);
    */
    $j(this).append(sfhh).append(sfpt);
    $j(this).append('<div class="morelink">'+more+'</div>');
    $j('.morelink',this).wrap($j('<a>').attr('href', '/blog/?p='+sid[1]));
});

There were all sorts of crazy things going on in that code. Remember that you need to modify the objects before appending them to another object (unless you have some unique way of identifying them after the fact, i.e. IDs).

Good luck.

KyleFarris