Answer the question
In order to leave comments, you need to log in
How to modernize the code for collapse elements so that a new id is substituted for each new element?
Hey! Tell me how to make such a "cycle", perhaps it can be called that. The layout has two blocks inside which there is hidden text - made using bootstrap collapse. A change in the text (More/Hide) is attached to the block expansion event. But there is a unique identifier for each block. If there were a limited number of blocks, it would be possible to write an id for each and hang an event on it. But there are a lot of elements, so you need to somehow systematize the addition of an id for a new element, and a bunch of id with a data element. Now for two blocks, the code in js is like this:
$('#toggle-cat-desc').on('show.bs.collapse', function(){
$(this).next().html('Скрыть');
});
$('#toggle-cat-desc').on('hide.bs.collapse', function(){
$(this).next().html('Подробнее');
});
$('#toggle-cat-desc2').on('show.bs.collapse', function(){
$(this).next().html('Скрыть');
});
$('#toggle-cat-desc2').on('hide.bs.collapse', function(){
$(this).next().html('Подробнее');
});
Answer the question
In order to leave comments, you need to log in
Just make a general class
$('.toggle-cat-desc').on('show.bs.collapse', function(){
$(this).next().html('Скрыть');
}).on('hide.bs.collapse', function(){
$(this).next().html('Подробнее');
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question