Get Element by Id and set the value in JavaScript

If myFunc(variable) is executed before the textarea is rendered to the page, you will get the null exception error.

<html>
    <head>
        <title>index</title>
        <script type="text/javascript">
            function myFunc(variable) {
                var s = document.getElementById(variable);
                s.value = "new value";
            }
            myFunc("id1");
        </script>
    </head>

<body>
    <textarea id="id1"></textarea>
</body>

</html>
<!-- Error message: Cannot set property 'value' of null -->

So, make sure your textarea does exist on the page, and then call myFunc. You can use the window.onload or $(document).ready function.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)