jquery - wrap image with next link -


i'm working on slideshow upcoming events. slides suppose link corresponding event page. but, not slides suppose have link if don't have event page.

here html i'm able output:

 <ul>   <li><img><a href="/link1">link1</a></li>   <li><img><a href="/link2">link2</a></li>   <li><img></li> // 1 doesn't have link  </ul> 

then jquery i'm trying change html this:

 <ul>   <li><a href="/link1"><img></a></li>   <li><a href="/link2"><img></a></li>   <li><img></li> // 1 doesn't have link  </ul> 

here jquery i've been working on:

$(function() {      $(' ul li img').each(function() { // each image          if($(' ul li img').next('a').attr("href").length) // check if banner has link          var = $(this).next('a').attr("href"); // find associated anchor , give name          $(' ul li img').wrap('<a href="' + + '"></a>'); // , wrap image          return false;  }); 

this seems take first href , wrap every image same href.

$(' ul li img') returns array, in .each(funtion(){}) must use $( ) returns current img in array.

you code must looks this:

$(' ul li img').each(function() { // each image   var $a = $(this).next('a').attr("href");   if($a.length){ // check if banner has link     $(this).wrap('<a href="' + $a + '"></a>'); // , wrap image   } }); 

not tested.


Comments

Popular posts from this blog

python - jinja2: TemplateSyntaxError: expected token ',', got 'string' -

Qt4: how to send QString inside a struct via QSharedMemory -

node.js - NodeJS remote terminal to Dropbear OpenWRT-Server -