php - Change HREF link depending on elements "value" in jquery -


i have following php returns records mysql table. these records displayed links. see code below...

        <div class="slide1" id="u1026">          <?php while ($row = mysql_fetch_array($query_rental)) {         echo "<a class='fancybox fancybox.iframe' id='rental' value={$row['layout']} href=\"brochures\items-rental.php?id={$row['client_name']}\"></a>";          }?>                           </div> 

what like, href link change to

\"brochures\items-rental-layout2.php?id={$row['client_name']}\

if value contains text "layout2". know can change href using jquery code

$(document).ready(function () {     $("#event").attr("href", "http://the.new.url") }); 

i'm not sure how depending if value contains text "layout2". appreciated. thanks

you can straight in php code:

while ($row = mysql_fetch_array($query_rental)) {   $layoutflag = $row['layout'] == 'layout2' ? '-layout2' : '';   echo "<a class='fancybox fancybox.iframe' id='rental' value=\"{$row['layout']}\" href=\"brochures\items-rental{$layoutflag}.php?id={$row['client_name']}\"></a>";  } 

you javascript:

$(function () {   // i'm assuming going turn rental class, otherwise change selector whatever.   $("a.rental").each(function() {     var rentalitem = $(this);     if (rentalitem.attr('value') === 'layout2') {       // can choose replace, long know replace want to. i'm going regex's ^ (start-of-line) operator make sure replacing @ start of line...       rentalitem.attr('href', rentalitem.attr('href').replace(/^brochures\\items\-rental/, 'brochures\\items-rental-layout2'));   }); }); 

as can see, doing in php easier.

also side note, creating multiple elements same id. maybe meant class='fancybox fancybox.iframe rental'?

and second side note, suggest using data- prefix holding custom data. in layout's case, use data-layout='layout-whatever'. can use .attr('data-layout') layout attribute (it's easier understand code doing too!).


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -