Manik
Manik

Reputation: 523

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 divs and as.

    <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

Upvotes: 0

Views: 79

Answers (2)

Manjeet Barnala
Manjeet Barnala

Reputation: 2995

Try this code, Here is working example try....

$(".text-center").find('.url1').each(function( i ) {
		var url1 = $(this).attr('href');
		$(this).parent().find('.url2').attr('href',url1);
	});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<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 class="url1" 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 url2" target="_blank">Change this to url1 </a>
		</div>  
	</span>	
</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 class="url1" href="url11111"><p class="name">Url</p></a>
	<span class="price">
		<div class="add-to-cart-button">
		<a href="url22222" rel="nofollow"  class="add_to_cart_button product_type_simple button alt-button small clearfix url2" target="_blank">Change this to url1 </a>
		</div>  
	</span>	
</div>

Upvotes: 1

Uzbekjon
Uzbekjon

Reputation: 11813

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'));
});

Upvotes: 1

Related Questions