Search code examples
javascriptgetelementbyid

Set value using elementbyid in Javascript


I am trying to set a value with Javascript using getElementById but it is not working. Can anyone see what I'm doing wrong? Sorry if this is basic question. Have not used JS in a long while.

<html>
    <head>
        <script>
            function setSymbol(sym) {
                //alert("hi");
                document.getElementById("personName")=sym;
            }
        </script>
    </head>
    <body onload="setName('John Ford')">
        <script type="text/javascript">
            new awesome.widget({
                "name": "<span id = personName>",
                "locale": "en"
            });
        </script>
    </body>
</html>

Thanks for any suggestions.


Solution

  • You are looking for .innerHTML or better yet .textContent:

    document.getElementById('personName').textContent = sym;