How do I get the value from a textbox and  assign it to a hiddenField  in JS,  using Html helper hidden field cod?   MVC4 ASP.NET

bmanmike39
bmanmike39 used Ask the Experts™
on
I have a textbox that I would like to assign the values to the hidden field, when i submit the form. How would I do this?  I'm using Html helper  and don't know how to 1, assign a id to it. And 2, how to access the textbox and put it in the hidden field.

Code:  js
<script type="text/javascript">
                    function gqt()
                    {
                        var x = document.getElementById("quantity").value;
                        
                    }

Open in new window


Code: view
<form method="post" action="script">

                   @Html.Label("Quantity:") <input id="quantity" type="text" value="1"/>
                   @Html.Hidden("itemNumber", ViewData["numberId"].ToString())
                   @Html.Hidden("quantity", ????)

                   <input class="btn btn-danger" type="submit" value="Add Info" onclick="gqt()" />
                </form>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Developer
Top Expert 2015
Commented:
Hi,
You need to change the ID for the hidden field as below.

@Html.Hidden("quantity", null, new {@id="qty" })

Open in new window


To assign and get the value using jQuery as follows.

<script type="text/javascript">

    function gqt()
    {
        var val = $("#quantity").val();

        $("#qty").val(val);

        alert($("#qty").val());
    }

</script>

Open in new window

Author

Commented:
don't I have to use jquery for this?
Lokesh B RDeveloper
Top Expert 2015

Commented:
Hi,

jQuery will be simple to use.

you can even use the JavaScript.

Author

Commented:
Thanks for the help!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial