0

Title says it all, i am setting a value in a variable in a javascript. I want to set that value as value of the one of the row in my form.

here is the code, this is based on previous similar questions and their accepted answers, but somehow it does not work for me.

<html>
<head>
<script>
    var counter = 0;
    var limit = 50;

    function addInput(divName){
         if (counter == limit)  {
              alert("You have reached the limit of adding " + counter + " inputs");
         }
         else {
              var newdiv = document.createElement('div');
              newdiv.innerHTML = "Element " + (counter + 1) + " <input type='text' name='myInputs[]'>";
              document.getElementById(divName).appendChild(newdiv);
              counter++;
         }
    }
</script>
</head>

<body>
<form method="POST"  align="center">
        <script>
        var emotion = "";
        var raw = Math.random();
        var final = Math.ceil(raw * 4);
        if (final == 1)
            document.getElementById("someid").value = "A";
        else if (final == 2)
            document.getElementById("someid").value = "B";
        else if (final == 3)
            document.getElementById("someid").value = "C";
        else if (final == 4)
            document.getElementById("someid").value = "D";
        </script>
     <div id="dynamicInput">
        Title<br><input type="text" value="" name="myInputs[]" id="someid" disabled>
     </div>
     <input type="button" value="Add another event" onClick="addInput('dynamicInput');">
     <br>
     <input type="submit" value="Submit">

</form>


</body>

</html>

I also tried something like:

document.forms[0].myInputs[0].value="Whatever"

inside the script tags, but that also does not work.

Edit_1 I expect some value in the disabled input field, just below the title, but its empty.

Here is the image:

enter image description here

How can I have some value based on a random number generator there?

3
  • what mean by does not work? any error? value did not show? Commented Sep 26, 2017 at 1:50
  • @Se0ng11, please see the edit. I apologize for not being very clear Commented Sep 26, 2017 at 1:56
  • 1
    the issue here is how u arrange ur script, u put ur script at the top of the html, which mean that it run the js 1st b4 the html ever appear, hence thats the issue Commented Sep 26, 2017 at 2:03

3 Answers 3

2

check the jsfiddle, this is due to how u arrange ur script, you either move ur code below the html, or use onload() event

example

onload="test()"

then you will no need to worry where the js should put

 <script>
        function test(){
        var emotion = "";
        var raw = Math.random();
        var final = Math.ceil(raw * 4);
        if (final == 1)
            document.getElementById("someid").value = "A";
        else if (final == 2)
            document.getElementById("someid").value = "B";
        else if (final == 3)
            document.getElementById("someid").value = "C";
        else if (final == 4)
            document.getElementById("someid").value = "D";
        }

 </script>

https://jsfiddle.net/gw04jhoj/

you can refer to this for more info Javascript that executes after page load

Sign up to request clarification or add additional context in comments.

Comments

1

Your script is running before those HTML elements have been created, and so setting their value will fail, as document.getElementById("someid") doesn't exist yet. You should see this fail in the error console in the browser (F12).

Try moving the script until after you've created the HTML elements in question, or better yet change your script to only execute once the document has loaded.

Comments

0

You should use :

document.getElementById("someid").value="whatever";

1 Comment

did you see the code? I tried that, and it does not work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.