Manik Manik - 7 months ago 6
Javascript Question

Change url based onanother link in jQuery

I need to make second url same as first url using jQuery. Please see my html code below. It is a list of

div
s and
a
s.

<div class="text-center">

<h5 class="category"><a href="url" rel="tag">No need to change this url</a> </h5>

<div class="tx-div small"></div>

<a href="url1"><p class="name">Url</p></a>

<span class="price">
<div class="add-to-cart-button">
<a href="url2" rel="nofollow" class="add_to_cart_button product_type_simple button alt-button small clearfix" target="_blank">Change this to url1 </a>
</div>


</div>


<div class="text-center">

<h5 class="category"><a href="url" rel="tag">No need to change this url</a> </h5>

<div class="tx-div small"></div>

<a href="url1"><p class="name">Url</p></a>

<span class="price">
<div class="add-to-cart-button">
<a href="url2" rel="nofollow" class="add_to_cart_button product_type_simple button alt-button small clearfix" target="_blank">Change this to url1 </a>
</div>


</div>


Please help

Answer

Updated code:

$('.text-center').each(function(){
  $('a:eq(2)', this).attr('href', $('a:eq(1)', this).attr('href'));
});

Try this code:

$('.fc').each(function(){
  $('.sec', this).attr('href', $('.fic', this).attr('href'));
});