How to get all the attributes of the tag a as an array?
<a data-key1="5" data-key2="7"></a> ja...">
How to get all the attributes of the tag a as an array?
<a data-key1="5" data-key2="7"></a> The method .data() is responsible for getting data-* attributes.
To get all the data-* attributes:
/* Получаем все свойства */ var data = $('#test').data(); /* Для демонстрации */ $('body').append($('<ul>').attr('id', 'result')); /* Выводим свойства */ for (var key in data) { $('<li>', { text: key + ': ' + data[key] }).appendTo('#result'); } <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <a id="test" data-key1="5" data-key2="7"></a> To get all the properties:
/* Получаем все свойства */ var attributes = document.getElementById('test').attributes; /* Для демонстрации */ $('body').append($('<ul>').attr('id', 'result')); /* Перебираем свойства */ for (var key in attributes) { /* Выбираем именно html-атрибуты */ if (attributes[key].nodeName) { $('<li>', { text: attributes[key].nodeName + ': ' + attributes[key].nodeValue }).appendTo('#result'); } } <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <a id="test" data-key1="5" data-key2="7"></a> Based on the answers:
id not output. - Grundydata-* attributes - VenZell.attr() , but did not work. - user199345attr - gives a specific attribute. You can also see the attributes property of the element - all attributes are stored in it. - GrundySource: https://ru.stackoverflow.com/questions/486518/
All Articles
data-*? or others are needed too? - Grundy