tags:

views:

52

answers:

2

Hello I have trying to wrap an image with anchor if class has anchor.

jQuery:

if ( $(".views-row:has(a)").length) {
    var noderef = $(this).attr("anchor");
    $(".views-field-field-teaserbox-fid img").wrap("[anchor = "'+noderef+'" ]");
}

HTML:

    <div class="view-content">
        <div class="views-row">

          <div class="views-field-field-node-ref-nid">
           <span class="field-content"><a class="active" href="/all-essentials-inspiring-events">All the essentials of inspiring events.</a></span>
         </div>

         <div class="views-field-field-teaserbox-fid">
            <span class="field-content"><img width="208" height="137" src="http://localhost:8888/sites/default/files/wedding_Giveaway_teaser.jpg?1283880578" alt="" class="imagefield imagefield-field_teaserbox"></span>
    </div>

Also, should I be using a double or single quotes?

A: 

How's this?

$("img.anchor").wrap('<a href="#" />');

ref: http://api.jquery.com/wrap/

Scott
A: 

I'm guessing you might want something like this:

$('.views-row').each(function(){
    var current = $(this);
    var anchor = current.find('a').first().attr('href');

    if(anchor){
      var wrapped = $(current.find('img')).wrap('<a href="' + anchor + '"></a>');
    }
});

This will wrap an anchor with the same href as .view-field-field-node-ref-id a around the img in .views-field-field-teaserbox-fid.

Oh, and about single or double quotes: It doesn't matter - use whatever works for you.

Yi Jiang
`.wrapAll()` wraps the element on the left of the function, so this would create some invalid HTML :)
Nick Craver
@Nick Oops... .
Yi Jiang
Yi, i tried your code and it isn't working. I will continue to debug
arkjoseph
I got it, .first() seemed to be causing an issue so i removed it.
arkjoseph