asp.net mvc 4 - Textchanged event in mvc for a Textbox -
i beginer in mvc.i working on application need change value of textbox after enter value in , press tab.for example when enter 1 in textbox , press tab should display +1 ie need concatinate + sign text enter.so trying raise textchanged event textbox.how can it.when googled found can done using javascript.but dont know how that,pls give me suggessions.
code:
@html.textboxfor(m => m.text)--textbox
you use jquery , subscribe blur
event triggered when input field looses focus:
$(function() { $('#id_of_your_textbox').blur(function() { var value = $(this).val(); var newvalue = value + '1'; $(this).val(newvalue); }); });
if don't want use javascript framework such jquery achieve same plain javascript:
window.onload = function() { document.getelementbyid('id_of_your_textbox').onblur = function() { var value = this.value; var newvalue = value + '1'; this.value = newvalue; }; };
note: add string value 1
existing text. if wanted perform integer addition need parse current value first. use parseint
or parsefloat
methods (depending on data type) that:
var value = parseint($(this).val()); if (!isnan(value)) { var newvalue = value + 1; $(this).val(newvalue); } else { alert('you have entered invalid integer value'); }
and here's how assign deterministic id text field use in javascript selectors:
@html.textboxfor(m => m.text, new { id = "id_of_your_textbox" })