How to change paragraph font size and color when on button click in JavaScript?

Asked 19-Jul-2021
Viewed 577 times

1 Answer


2

<!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');
            var size = document.getElementById('inp');
        //alert('Your selected color is : ' + name.value);
        name.style.backgroundColor = name.value;
        window.document.body.style.backgroundColor = name.value;
        document.getElementById('txt').style.fontSize = size.value+'px';
    }
    </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>
    <input id='inp' type='number' min='10' max='200' placeholder='Enter font size'/>
    <button type='button' onclick='changeColor()' value=' Submit ' id='sub'> Submit </button>
    <p id='txt'>this is demo of txt </p>
</body>
</html>