javascript - Pass text of input to calling function (yes it is possible) -
i know question has been asked -and answered- multiple times. found new solution - not understand it. setup this:
<input id="input1" onchange="gettext()"/>
all answers found suggest use id value of input.
function gettext(){ alert($("#input1").val()); }
$(this).val() not work here.
another way use value of #input1 use this.value in calling function:
<input id="input1" onchange="gettext(this.value)" />
this passes value parameter function.
however found jquery sample attaches function #input1 , makes $(this).val() work.
$("#input1").change(function(e){ alert($(this).val()) });
against answers here @ stackoverflow seeing possible attach function input field , have access value of - ask myself how have write function , not attach jquery. or can attached jquery? why?
here fidle setup play
you either pass reference input object parameter in inline call callback this:
<input id="input2" onchange="gettext(this)" />
and in javascript:
function gettext(_this){ alert(_this.value); }
or can attach function directly input object so
document.getelementbyid('input2').gettext = function() { alert(this.value); };
and in html:
<input id="input2" onchange="this.gettext()" />
basically object in javascript bound context, in function has been created. when define function globally, gettext in example, bound global object in scope of function.
Comments
Post a Comment