Question

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=""/>
Was it helpful?

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());
    });
});

OTHER TIPS

view:

    <input type="button" id="test" value="test" />
    @Html.TextBoxFor(m => m.Email, new { @placeholder = "Email" , @id="EmailTxt"})

JavaScript:

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

Instead of

@id="EmailTxt"

we can write

id="EmailTxt"
@Html.TextBoxFor(m => m.Email, new { @placeholder = "Email" , id="EmailTxt"}).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top