最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

changing the data-icon in pure javascript - Stack Overflow

programmeradmin2浏览0评论

How would I update the data-icon attribute in Javascript?

document.getElementById("btn").data-icon = "&#xe082";

This does not work, as it would for style, or width or whatever.

How would I update the data-icon attribute in Javascript?

document.getElementById("btn").data-icon = "&#xe082";

This does not work, as it would for style, or width or whatever.

Share Improve this question asked Nov 21, 2013 at 22:14 wazzadaywazzaday 9,6746 gold badges45 silver badges69 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Use .setAttribute(); for setting attributes to elements

document.getElementById("btn").setAttribute("data-icon", "&#xe082");

Also, you cannot use - when calling objects as it thinks you are trying to subtract, unless you call it like obj['key-name'];

It's an attribute, and for attributes it's setAttribute()

document.getElementById("btn").setAttribute('data-icon', '&#xe082');

Use .setAttribute(); for setting attributes to elements:

document.getElementById("btn").setAttribute("data-icon", "&#xe082");

Also, you cannot use - when calling objects, as it thinks you are trying to subtract, unless you call it like obj['key-name'];.

发布评论

评论列表(0)

  1. 暂无评论