https://codepen.io/st-iv/pen/LBEpzg

After clicking "view: table", you need a check mark instead of the text "Goods in the basket" (for example, this is just the letter V). Tried to make selectors, see what's wrong.

$('.btn_buy').click(function(e){ e.preventDefault(); $(this).html('Товар в корзине').attr('href', '/basket/'); }); $('.view').click(function(e){ e.preventDefault(); $('.goods__item').addClass('view-table'); }); $('.goods__item .view-table .btn_buy').click(function(e){ e.preventDefault(); $(this).html('V').attr('href', '/basket/'); }); 
  • add html markup to the question so that you can run your code - Anatoly Shevelev
  • I attached the link to the codepen at the beginning - stiv

1 answer 1

You have two problems here.

  1. You are trying to hang a handler on a non-existent element.
  2. An extra space in the selector.

This is how it works.

 $('.btn_buy').click(function(e){ e.preventDefault(); $(this).html('Товар в корзине').attr('href', '/basket/'); }); $('.view').click(function(e){ e.preventDefault(); $('.goods__item').addClass('view-table'); var table_btn = $('.goods__item.view-table .btn_buy'); table_btn.each(function(){ if ($(this).html() == 'Товар в корзине'){ $(this).html('V') } }); table_btn.click(function(e){ e.preventDefault(); $(this).html('V').attr('href', '/basket/'); }); }); 

  • cool. But I did not take into account one more thing: click on the button "in the basket" - now it turns out "goods in the basket." We go to the "view: table" and the "item in the basket" remains, but it is necessary that there be a tick "V". Here, without your help, I still can not :( - stiv
  • @StepanIvanov Added check. - Hikikomori