2

I'm trying to add a JavaScript variable to a hidden input field. How would I do this?

HTML:

<button type="button" class="submitFormOnClick btn btn-warning pull-right"
        style="margin-bottom:15px; margin-top:15px;" id="bet">Bet
</button>

<input type="hidden" name="controller" value="keno">
<input type="hidden" name="task" id="task" value="">
<input type="hidden" name="pickednumbers" id="pickednumbers">

JavaScript:

<script type="text/javascript">
    var inc = 0;
    var pickednumbers = [];
    function myFunction(elmnt, color, id)
    {
        if(pickednumbers.length >= 20)
        {
            return false;
        }

        if (pickednumbers.indexOf(id) === -1)
        {
            pickednumbers.push(id);

            elmnt.style.background = color;
            inc = inc + 1;
            if(inc >= 20)
            {
                document.getElementById('onColor').setAttribute("style", "background-color:green");
            }
        }
        else
        {
            return false;
        }
    }
</script>

What I need: All I want is to have the variable pickednumbers be sent with the input type hidden.

4

1 Answer 1

4

You may simply do this :

var inc = 0;
var pickednumbers = [2,3,4,8];

document.getElementById("pickednumbers").setAttribute("value",JSON.stringify(pickednumbers));
<button type="button" class="submitFormOnClick btn btn-warning pull-right" style="margin-bottom:15px;margin-top:15px;" id="bet">Bet
</button>

<input type="hidden" name="controller" value="keno">
<input type="hidden" name="task" id="task" value="">
<input type="hidden" name="pickednumbers" id="pickednumbers">

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

Comments

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.