Mike
Mike

Reputation: 2339

onchange event to html.editfor

I have an input box that is created in razor using Html.EditorFor.

@Html.EditorFor(model => model.person.Person.FirstName)

I would like to add a change event to this box so that some code will trigger if the value changes.

I have tried this jquery:

$('#person_Person_FirstName').change(function () {
    alert($(this).val());
}).change();

But I'm not getting anything.

Does anyone have any ideas?

Upvotes: 1

Views: 2931

Answers (3)

Mike
Mike

Reputation: 2339

adding $(document).ready(function(){ //...above code here }) worked

Upvotes: 0

Olrac
Olrac

Reputation: 1537

Try this code:

$(function() {

    $(document).on('change','#person_Person_FirstName', function() {
       alert($(this).val());
    });

});

Upvotes: 3

Eddie Monge Jr
Eddie Monge Jr

Reputation: 12730

If that is the actual id of that rendered element, then doing this should work:

jQuery(function($) {
  $('#person_Person_FirstName').change(function () {
    console.log( this.value );
  }).change();
});

Upvotes: 0

Related Questions