Search code examples
c#javascriptasp.net-mvc-3eventsonblur

Can't get event blur with asp.net mvc 3


Hi guys I've struggling how to get the event blur from a asp.net mvc 3 strongly typed view. This is what i've tried.

<table>
 <td class="contentTable">
     @Html.LabelFor(model => model.Ciudad)
  </td>
  <td>
     @Html.EditorFor(model => model.Ciudad, new{id= "mapSearch"})
     @Html.ValidationMessageFor(model => model.Ciudad)
 </td>
</table>


<script type="text/javascript">
    $(function () {
        $('#mapSearch').blur(function () {
           myFunction();
       });
});
</script>   

myFunction is already a function that I'm using fired by a button, but now I wanted it fired by an event. I've also tryed with:

@Html.EditorFor(model => model.Ciudad, new{onblur= "mapSearch();"})

Thanks


Solution

  • Try this. Change your View Markup like this

     @Html.EditorFor(model => model.Ciudad)
    

    and script like this

    $(function () {
        $('#Ciudad').blur(function () {
               myFunction();
        });
    });
    

    Assuming you have a javascript function called myFunction exist

    function myFunction() {
            console.debug("myFunction Called");
           //Some other awesome stuff here
    }
    

    JsFiddle sample : http://jsfiddle.net/C3wMY/4/