user43153 user43153 - 2 months ago 8
HTML Question

Passing values into a external function JQuery

I tried to create external general function for all my document. That function I want to use "this" method to work inside process of function and that "this" value come from difference buttons. But this code is not working for me. how to solve it out.

jQuery Code:

function myFunction(e){
var inp = $(this).val(), target = e.target, position = target.selectionStart;
inp = inp.replace(/a/g, "A");
inp = inp.replace(/i/g, "I");
inp = inp.replace(/u/g, "U");
inp = inp.replace(/e/g, "E");
$(this).val(inp);
$(this).get(0).setSelectionRange(position, position );
}


Send the value to external function using jQuery

$(document).ready(function(){
$('#Search').keyup(myFunction('#Search'));
});


HTML Code:

<form name="inputform">
<input class="Search" type="text" autofocus name="searchbox" id="Search" style="width:800px; height:200px;">
</form>


If I put directly text box id it is working fine.

Answer

You don't need to pass #Search as argument of myFunction like myFunction('#Search'). You can do it simply like following.

$('#Search').keyup(myFunction);
Comments