How to change background color when button is clicked in JavaScript?

0 votes
39 views
Ravi Vishwakarma asked 13 days ago in Web Application by Ravi Vishwakarma

How to change background color when button is clicked in JavaScript?

1 Answer

1 votes
Ethan Karla answered 10 days ago by Ethan Karla
<!DOCTYPE html>

<html lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head>
    <meta charset='utf-8' />
    <title>JS Demo</title>
    <style type='text/css'>
        * {
            margin:10px;
            padding:0px;
        }
        select {
            width:100px;
        }
    </style>
    <script type='text/javascript'>
        function changeColor() {
        var name = window.document.getElementById('color');
        //alert('Your selected color is : ' + name.value);
        name.style.backgroundColor = name.value;
        window.document.body.style.backgroundColor = name.value;
        window.document.getElementById('sub').style.backgroundColor = name.value;
    }
    </script>
</head>
<body>
    <span>Select Color name : </span>
    <select name='color' id='color' >
        <option value='red'>Red</option>
        <option value='yellow'>Yellow</option>
        <option value='white'>White</option>
        <option value='pink'>Pink</option>
        <option value='green'>Green</option>
    </select>
    <button type='button' onclick='changeColor()' value=' Submit ' id='sub'> Submit </button>
</body>
</html>