There is a catalog of models with the ability to select each model with a button, when you click on a button, the model name is added to the array (the div with the name is in the same block with the button), when you press the button again, the name should be removed from the array, tell me how to do this? Names can be repeated. At the moment, it is only possible to add model names to the array:
jQuery(function() { var models = []; $('.mc-block .mc-change-button').on('click', function () { $(this).toggleClass('checked'); if (!$(this).hasClass('checked')) { $(this).html('Выбрать модель'); models.splice(models.indexOf($(this).parent().find('.model-name').html()), 1); alert(models); } else { $(this).html('Модель выбрана'); models.push($(this).parent().find('.model-name').html()); alert(models); } }); }); .mc-block { padding: 10px 10px; border: solid 1px #000; } .mc-change-button.checked { color: red; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="mc-block"> <div class="model-name"> Александра 01 </div> <a class="mc-change-button">Выбрать модель</a> </div> <div class="mc-block"> <div class="model-name"> Александра 01 </div> <a class="mc-change-button">Выбрать модель</a> </div> <div class="mc-block"> <div class="model-name"> Александра 01 </div> <a class="mc-change-button">Выбрать модель</a> </div> <div class="mc-block"> <div class="model-name"> Александра 05 </div> <a class="mc-change-button">Выбрать модель</a> </div> <div class="mc-block"> <div class="model-name"> Александра 04 </div> <a class="mc-change-button">Выбрать модель</a> </div> <div class="mc-block"> <div class="model-name"> Александра 02 </div> <a class="mc-change-button">Выбрать модель</a> </div> JSFiddle https://jsfiddle.net/6k0k4f0y/
splice- Grundy