admin管理员组文章数量:1410724
I am trying to make an onclick button to change the background color of a div, using its id. My JavaScript function is as follows:
function changeButton(color) {
document.getElementById('button1').style.backgroundColor = color;
}
and I'm linking to it in the HTML file like this:
<script> src="websiteCleanJS.js" </script>
.
.
.
<div id='button1' onclick="changeButton('red')"> </div>
I am trying to make an onclick button to change the background color of a div, using its id. My JavaScript function is as follows:
function changeButton(color) {
document.getElementById('button1').style.backgroundColor = color;
}
and I'm linking to it in the HTML file like this:
<script> src="websiteCleanJS.js" </script>
.
.
.
<div id='button1' onclick="changeButton('red')"> </div>
Share
Improve this question
edited Mar 9, 2015 at 15:18
Mahmud Ahmad
asked Mar 9, 2015 at 15:11
Mahmud AhmadMahmud Ahmad
1713 silver badges18 bronze badges
8
-
4
<div id='button1', onclick('red')>
– that is total nonsense and miles away from valid syntax. You need to go learn some basics. – C3roe Commented Mar 9, 2015 at 15:12 - start from here, pleas: developer.mozilla/en-US/Learn/Getting_started_with_the_web/… – rvandoni Commented Mar 9, 2015 at 15:13
- In an odd twist your JavaScript syntax is fine but your HTML is wrong. – j08691 Commented Mar 9, 2015 at 15:14
- Accidentally copied it over wrong, sorry! @CBroe – Mahmud Ahmad Commented Mar 9, 2015 at 15:16
-
Your click syntax is still incorrect. Should be
onclick="changeButton('red')"
– j08691 Commented Mar 9, 2015 at 15:17
1 Answer
Reset to default 4try this:
<script src="websiteCleanJS.js"></script>
.
.
.
<div id='button1' onclick="changeButton('red')"> </div>
本文标签: htmlChanging background color using JavaScript using idStack Overflow
版权声明:本文标题:html - Changing background color using JavaScript using id - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744792502a2625375.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论