views:

358

answers:

1

I'm nearly where I want to be, except that I can't figure out how to turn var imgt into a hyperlinked image. I've tried some things, but it keeps returning [object][Object]

$j('#hp-featured-item > div[id^="post-"]').each(function() {
      var id=this.id.match(/post-(\d+)/);
      var imgt = $j("img:eq(0)");

      // I tried this but it didn't work
      // var imgt = $j("<a href='/blog/?p="+id[1]+"'>"+$j("img:eq(0)")+"</a>");

      var pt = $j("p:not(:has(img)):eq(0)");
      var hh = $j("h2:eq(0)");
      $j(this).html('');
      $j(this).append(imgt).append(hh).append(pt);
});

/// Updated code

$j('#hp-featured-item > div[id^="post-"]').each(function() {
                var id=this.id.match(/^post-([0-9]+)$/);
                var imgt = $j("img:eq(0)");
                $j(imgt).wrap($j('<a>').attr('href', '/'));
                var pt = $j("p:not(:has(img)):eq(0)");
                var hh = $j("h2:eq(0)");
                $j(this).html('');
                $j(this).append(imgt).append(hh).append(pt);
        });

Ok, I put it in the comment, but I'll put it here too based on your suggestion... Essentially, the original html output varies greatly in structure. Sometimes it's

<img /> 
<h2> </h2> 
<p> </p>
<p> <img /> </p>
<h2> </h2>
<p> </p>

Sometimes, it's just:

<h2> </h2>
<p><img /></p>
<p> text </p>

etc...

I want to pull the first <img />, the first <h2>, and the first <p> (that isn't wrapped around an image) and print them out in that order:

<img />
<h2>
<p>

And, the rest can just go away... It's just a summary view...

Well, ok... I added this to the bottom, and it seems to work:

$j(this).each(function() {
                        var img = $j('img');
                        $j(img).wrap($j('<a>').attr('href', '/'));
                });
+1  A: 

You can use jQuery.prototype.wrap for this:

$j('#hp-featured-item > div[id^="post-"]').each(function() {
      var id=this.id.match(/post-(\d+)/);
      var imgt = $j("img:eq(0)");

      $(imgt).wrap( 
          $('<a>').attr('href', '/blog/?p=' + id[1])
      );

});

This worked for me:

$('.post').each(function() {
    var img = $('img', this), src = $(img).attr('src')
    $(img).wrap( 
             $('<a>').attr('href', src)
    );
});

http://jsbin.com/iyabu

Make sure the href attribute you set it to is right.

meder
This isn't working for me... I tried different variations, including what you have above... I tried:<pre>$j(imgt).wrap( $j('<a>').attr('href', '/blog/?p=' + id[1]));</pre>and <pre>imgt.wrap('<a href="/blog/?p=' + id[1] + '"></a>');</pre>But neither had any effect...
n00b0101
I updated my example. Stop making it complex if you can't get it and try simplifying it as much as possible, it might be that your array doesn't contain 2 elements, use a static href value to test.
meder
small point of note: I'd use `/^post-([0-9]+)$/` for your regex so it doesn't match "blahpost-123blah"
nickf
By the way, you *are* getting rid of your manual .html() and .append stuff? Can you please provide the updatd code.
meder
Tried the second example, and it's still just returning the <img> html with no wrapper. The html that's originally produced has many <img>, but I'm only interested in the first one it finds, thus the var imgt = $j("img:eq(0)");I tried:$j(img).wrap( $j('<a>').attr('href', '/') ); and var imgt = $j("img:eq(0)").wrap( $j('<a>').attr('href', '/') );But, it's just returning <img ... /> I'm using the jquery noconflict thing, which is why I've got the $j in there...
n00b0101
Yes, I'm emptying the html because there's a bunch of additional stuff that I don't want to display...
n00b0101
I put the updated code in the original question
n00b0101
Then that's the problem, .wrap wraps it IMMEDIATELY, I suggest you use .wrap and get rid of stuff with .remove or .hide but if you're going to .append you will need to do it correctly and it's harder, that and you haven't shown any HTML source so we can't easily see what you want to append in what order..
meder
You should probably update your entire original description with what you *really* want to do.
meder
I'm not sure what you mean by not showing any HTML source. In its original form, it may look like this: <div id="hp-featured-item"><div id="post-6"><h2>Some title</h2><p>Some paragraph</p><img src="/someimage.jpg" /><p>another paragraph</p><p><img src="/anotherimage.jpg" /></p><blockquote>some stuff</blockquote> etc., etc. It varies... But, what I'm trying to do is pull the *first* <img>, the *first* <h2>, and the *first* <p> and place them in that order. <img> <h2> <p> so that it looks like this:<img ... /><h2> title </h2><p> paragraph </p>And just dump the rest...
n00b0101
Can you edit your *original* post with the full source please, so it's easier to read?
meder
Yes... I posted the comment before I saw your last comment... sorry!
n00b0101