jQuery changing links of each child inside an each loop -


i trying cid of each .vehicleid , append each href inside .vehicle-actions class &vid=[cid]

<ul>             <li><a href="./garage_vehicle.php?mode=add_vehicle"><span>create vehicle</span></a></li>              <li class="vehicle-view togglevehiclebutton"><a class="vehicleid" href="./garage_vehicle.php?mode=view_own_vehicle&amp;cid=6"><span>2014 audi a4</span></a>                 <div class="vehicle-actions" style="width: 179px; display: none;">                     <ul>                         <li><a href="/board/garage_vehicle.php?mode=view_vehicle">view vehicle</a></li>                         <li><a href="/board/garage_vehicle.php?mode=edit_vehicle">edit vehicle</a></li>                         <li><a class="deletevehicle" href="">delete vehicle</a></li>                         <li><a href="/board/garage_modification.php?mode=add_modification">add modification</a></li>                         <li><a href="/board/garage_premium.php?mode=add_premium">add insurance</a></li>                         <li><a href="/board/garage_quartermile.php?mode=add_quartermile">add quartermile time</a></li>                         <li><a href="/board/garage_dynorun.php?mode=add_dynorun">add dynorun</a></li>                         <li><a href="/board/garage_track.php?mode=add_lap">add lap</a></li>                         <li><a href="/board/garage_service.php?mode=add_service">add service</a></li>                         <li><a href="/board/garage_vehicle.php?mode=view_own_vehicle">set main vehicle</a></li>                     </ul>                 </div>             </li>              <li class="vehicle-view togglevehiclebutton"><a class="vehicleid" href="./garage_vehicle.php?mode=view_own_vehicle&amp;cid=7"><span>2008 chevrolet corvette</span></a>                 <div class="vehicle-actions" style="width: 179px;">                     <ul>                         <li><a href="/board/garage_vehicle.php?mode=view_vehicle">view vehicle</a></li>                         <li><a href="/board/garage_vehicle.php?mode=edit_vehicle">edit vehicle</a></li>                         <li><a class="deletevehicle" href="">delete vehicle</a></li>                         <li><a href="/board/garage_modification.php?mode=add_modification">add modification</a></li>                         <li><a href="/board/garage_premium.php?mode=add_premium">add insurance</a></li>                         <li><a href="/board/garage_quartermile.php?mode=add_quartermile">add quartermile time</a></li>                         <li><a href="/board/garage_dynorun.php?mode=add_dynorun">add dynorun</a></li>                         <li><a href="/board/garage_track.php?mode=add_lap">add lap</a></li>                         <li><a href="/board/garage_service.php?mode=add_service">add service</a></li>                         <li><a href="/board/garage_vehicle.php?mode=view_own_vehicle">set main vehicle</a></li>                     </ul>                 </div>             </li>         </ul> 

in head seems simple can't seem grasp i'm doing wrong

jquery

function geturlparameter(url, name) {     return (regexp(name + '=' + '(.+?)(&|$)').exec(url)||[,null])[1]; }  $('.vehicleid').each(function(i, val){     var vehicleidurl = ($(this).attr('href'));     var cid = geturlparameter(vehicleidurl, 'cid');     $('.vehicle-actions',this).each(function(i, val){         editcid = $(this).find('a');         editcid.attr('href', editcid.attr('href') + '&vid=' + cid);     }); }); 

you inside a.vehicleid element should inside parent li.

your code can simplified. :

$('.vehicleid').each(function(i, val){     var cid = geturlparameter(this.href, 'cid');     $('.vehicle-actions a', $(this).closest('li')).attr('href', function(_, href){          if (href.indexof("&vid")!=-1) return href;           return href + '&vid=' + cid);     }); }); 

among simplifications, prefer this.href on $(this).attr('href') , use the version of attr function taking function second parameter.


Comments