0

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

2
  • 2
    The question is missing the attempted code, also somewhat unclear. edit. Commented Apr 23, 2016 at 12:46
  • Have a look at addClass and removeClass. Commented Apr 23, 2016 at 12:49

2 Answers 2

1

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'));
});
Sign up to request clarification or add additional context in comments.

2 Comments

Looks the same except css class names. Just change css class names in my code and everything should work just fine.
But i can't add class name . And it don't have class name .
1

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>

6 Comments

But it is a loop of html div . It is repeating .
Thank you :) .. But url1 is always change . And it is dynamic . Please see the accepted answer .
@Manik i've edited the answer, please check use common class in for anchor in loop and then .each anchor will solve your problem, try the code above...
Thank you :) .. But url1 is always change and there is no class namely url1 . And it is dynamic . Please see the accepted answer
@Manik this will also works for dynamic urls, i'm here getting the href of anchor from class...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.