0

I have a list of names that I want to randomize and have been able to get it to work, bit I also need these names to load in an text input field.

The code below works, but value="p id="demo1"/p" doesnt make it appear in the text input.

Thanks

    <input type="text" id="input_7" name="q7_def" data-type="input-textbox" class="form-textbox" size="20" value="" placeholder="" data-component="textbox"/>

        <body 
        onload="document.getElementById('demo1').innerHTML =( getRandomName1() );">
    </body>


    <p id="demo1"></p> 


        <script>
        function getRandomName1()
        {
            var names = [
                '1111',
                '2222',
                '33333',
                '44444'
            ];

            return names[Math.floor(Math.random() * names.length)];
        }
    </script>
Nel
  • 13
  • 4
  • Inline event handlers are essentially `eval` inside HTML markup - they're bad practice and result in poorly factored, hard-to-manage code. Seriously consider attaching your events with JavaScript, instead, eg: https://developer.mozilla.org/en/DOM/element.addEventListener – CertainPerformance Apr 14 '18 at 00:45
  • Possible duplicate of [Getting a random value from a JavaScript array](https://stackoverflow.com/questions/4550505/getting-a-random-value-from-a-javascript-array) – Aluan Haddad Apr 14 '18 at 00:45
  • I know how to get the random value, thats why its not a duplicate. I am having trouble making the input value="" the random value that I get. – Nel Apr 14 '18 at 01:00
  • @Nel oh I see, sorry about that. Anyway, you're setting the wrong property. It's called `value` not `innerHTML` – Aluan Haddad Apr 14 '18 at 01:19
  • @AluanHaddad Thanks. If I change that, how do I specify which input box it goes in – Nel Apr 14 '18 at 01:26
  • I figured it out, thanks for the help – Nel Apr 14 '18 at 02:01

0 Answers0