How do I read an attribute of a HTML tag using JavaScript?
I have a <span>
element that I am reading with JavaScript. The element is like the following one:
<span key="789">
<input id="ctl00_cphBody_gvContact_ctl09_cbox" type="checkbox" name="ctl00$cphBody$gvContact$ctl09$cbox">
</span>
I need to get the value for the "key" attribute. If I try to get the inner HTML of the HTML element, it开发者_运维知识库 returns the HTML of the inner checkbox.
Using jQuery:
$('span').attr('key').
Using pure javascript
document.getElementsByTagName('span')[0].getAttribute('key')
JQuery makes it really easy. See the .attr method
You'll probably want to assign an id or class to that span so that you can select it specifically:
<span id="idOfSpan" key="blah">...</span>
var spanKey = $('#idOfSpan').attr('key')
精彩评论