Question

I'm newbie in JavaScript and I want to change the input element text Color but nothing happen when I load the page where I made a Mistake ?

<head>
<script language="text/javascript">
        function chngcolor()
        {
            var x=document.getElemetById('status')[0].value;
            if(x=='Accept')
            {
                x.style.color = '#00FF00';

            }

        }
</script>
</head>

<body onload="chngcolor();">

<form>
<input type='text' name='status' id='status' value="Accept" >
</form>


</body>
Was it helpful?

Solution

This will work:

function chngcolor() {
    var x = document.getElementById('status');
    if (x.value == 'Accept') {
        x.style.color = '#00FF00';
    }
}

getElementById returns single DOMElement, so you don't need [0]. You also misspelled getElemetById.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top