Hello.
Everyone is familiar with the .toggle() and .toggleClass() functions that swap styles and element classes:
$(function() { $('.block').click(function() { $(this).toggleClass('block_new-style'); }); }); .block {width: 200px; height: 200px; background-color: red} .block.block_new-style {background-color: green} <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="block"></div> But is it possible to somehow replace the attribute name or value in this way?
For example:
There is an element
<div class="block" data-style="0"></div>, when you click on it, its value of thedata-styleattribute is changed to1and when it is pressed again to0
Or:
There is an element
<div class="block" data-style_0></div>, when you click on it, its name attributedata-style_0changed todata-style_1and when you click again ondata-style_0, but if it is there is some value, then it remained unchanged
Who implemented it? I would be very grateful if you share your thoughts. Thank you in advance.
toggleandtoggleClass? or by writing your own function? - GrundytoggleandtoggleClasswork with only two possible values: true / false. How do you want to determine which values to use for attributes? That is, why thedata-style="0"should be replaced bydata-style="1"? is this the only possible case? or values in individual cases may differ? - GrundytoggleAttr('название атрибута', 'значение1', 'значение2')toggleAttr('название атрибута', 'значение1', 'значение2')- Yuridata-style_0doesn’t fall under this calldata-style_0When you click on it, itsdata-style_0attribute name is changed todata-style_1- what will you transfer the name as a parameter and what values? - Grundy