سؤال

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>
هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top