Answer the question
In order to leave comments, you need to log in
How to use js to get the value of the href attribute of one link and assign it to another?
Hello
Several sections with different products, in the section there are two buttons "buy" and "add to cart".
The products are different, so the href attribute of the "add to cart" buttons is different.
How to use js to copy the value of the href attribute of the "add to cart" button to the href of the "buy" button, taking into account their parent section?
Those. so that when you click on "buy" the product is added to the basket!
The idea is that after adding the product to the cart with the "buy" button, then redirect to the order page, and leave the behavior of the "add to cart" button unchanged, i.e. just add to cart.
<section class="buy_and_to_card">
<a href="" class="checkout-button">купить</a>
<a href="/?add-to-cart=103" class="checkout-button">в корзину</a>
</section>
<section class="buy_and_to_card">
<a href="" class="checkout-button">купить</a>
<a href="/?add-to-cart=111" class="checkout-button">в корзину</a>
</section>
Answer the question
In order to leave comments, you need to log in
If it's easier to do:
<section class="buy_and_to_card">
<a href="/?add-to-cart=111" class="checkout-button checkout-button-buy">купить</a>
<a href="/?add-to-cart=111" class="checkout-button">в корзину</a>
</section>
$('checkout-button').on('click', function() {
// отправляем ajax запрос на добавление
// при success проверяем есть ли класс checkout-button-buy у элемента и решаем что дальше делать
})
The easiest way is to use jquery:
$(document).on( "click", 'a.checkout-button', function(e){
// Скрипт добавления в корзину.
e.preventDefault(); // отключение перехода по ссылке
});
<section class="buy_and_to_card">
<a href="" class="checkout-button">купить</a>
<a href="/?add-to-cart=103" class="checkout-button add-button">в корзину</a>
</section>
$(document).on( "click", 'a.checkout-button', function(e){
switch(true){
case ($(this).is('.add-button')): // Тут код для действия "в корзину". $(this).attr('href') даст значение ссылки
break;
default: //Тут код для действия "купить"
}
e.preventDefault();
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question