Display all set attributes of an element

I have a jquery object that represents an input button element on a page. How can I with jquery output through console.log all the properties / attributes of this element?

+3
source share
2 answers

Assuming HTML Pages

<body>
  <img id="smile" class="big" alt="smile" madeupattribute="yep" src="http://mikegrace.s3.amazonaws.com/forums/stack-overflow/smile.png"/>
</body>

you could do

var domElement = $("img")[0] // [0] returns the first DOM element that jQuery found
$(domElement.attributes).each(function(index, attribute) {
  console.log("Attribute:"+attribute.nodeName+" | Value:"+attribute.nodeValue);
});

Page example => http://mikegrace.s3.amazonaws.com/forums/stack-overflow/example-get-element-attributes-jquery.html

Page Console Output Example

alt text

+6
source

If you want only HTML attributes:

var e = document.getElementById('my_input');
for (var x in e)
  if (e.hasAttribute(x))
    console.log(x);

If you want all the properties that can be obtained / set using JavaScript:

var e = document.getElementById('my_input');
for (var x in e)
  if (typeof e[x] != 'function')
    console.log(x);

JSBin - - Firefox typeof e['selectionStart'].

+1

Source: https://habr.com/ru/post/1777816/


All Articles