jqueryasp.net-mvchtml.textboxfor

MVC read value from textboxfor with jquery


I think it´s pretty simple, but i don´t get it. I want to read the value from an input control with jquery.

I have this line:

@Html.TextBoxFor(m => m.Email, new { placeholder = "Email" })

If I try to read the value with this line, i will get back an empty string

$("#Email").val()

If i try something like this, it works

$("#Email").keyup(function () {
        alert(this.value);
    });

I hope someone can help me. Thanks

Edit: Html

<input name="Email" id="Email" type="text" placeholder="Email" data-val-required="field is required" data-val="true" value=""/>

Solution

  • Did you add a reference to jQuery?

    I tested in JSFiddle and it works: demo

    HTML:

    <input name="Email" id="Email" type="text" placeholder="Email" data-val-required="field is required" data-val="true" value=""/>
    <input type="button" id="test" value="test" />
    

    JavaScript:

    $(document).ready(function () {
        $("#test").click(function () {
            alert($("#Email").val());
        });
    });