D
D
Dima Samsonov2018-02-06 11:06:52
JavaScript
Dima Samsonov, 2018-02-06 11:06:52

Is it possible to change the class of the button and its name through (if else)?

Good morning everyone.
need a little help.
There are two blocks on the page.

<div class="single-goods">
<p class="soh-goods">на складе</p>
<button class="add-to-cart">Купить</button>
</div>

<div class="single-goods">
<p class="soh-goods">нет на складе</p>
<button class="add-to-cart">Купить</button>
</div>

Can you please tell me if it is possible to make some kind of check to change the class and name of the button ??
something like this:
If(if $('.soh-goods') == 'out of stock') {
then button class="add-to-cart" assign class="add-to-soh"
and change the title buttons on 'order'
}
else{
otherwise do nothing.
}.
I will be very grateful for your help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Gleb Starkov, 2018-02-06
@dima_toster

$(document).ready(function(){
    $('.soh-goods').each(function(){
        if ($(this).html() == 'нет на складе') {
           $(this).closest('.single-goods').find('.add-to-cart').removeClass('add-to-cart').addClass('add-to-soh').html('Заказать');
       }
    });
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question