Issue
I'm having trouble changing the color of a button with a simple function, the color doesn't change at all.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script language="JavaScript">
function changeColor(){
document.getElementsByTagName('button').style.backgroundColor="green";
}
</script>
</head>
<body >
<form action="/action_page.php" method="get" name="form1">
<input type="text" id="campoDeFlores">
<button type="button" onclick="changeColor()" name="1">1</button>
<button type="button" name="2">2</button>
<button type="button" name="3">3</button>
</form>
</body>
</html>
Why does it not work?
Solution
document.getElementsByTagName
returns an list of elements not a single element. You need to convert it to an array with Array.from
and then iterate over the buttons with Array.map
function changeColor(){
Array.from(document.querySelectorAll('button')).map(function(button) {
button.style.backgroundColor="green";
})
}
Answered By - Hum4n01d
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.