Reputation: 331
when i press enter key on txtZip textbox It MOve to other page rather then calling JS i wanna set another text box value eqaul to txtZip
<asp:TextBox Style="text-align: center" ID="Txt_Zip" runat="server" Width="120px"
Text="Zip" onkeypress="return runScript(event)"></asp:TextBox>
<asp:TextBox Style="text-align: center" ID="Txt_Second" runat="server" Width="120px"
Text="Zip"></asp:TextBox>
function runScript(e)
{
if (e.which == 13 || e.keyCode == 13)
{
var zip =document.getElementById("txtZip").value;
document.getElementById("txt_Second").value=zip;
}
}
as i ahve one button also that re direct to another page on click how to restrict that on pressing enter key on text box just call JS ??
Hopes for your suggestions
Thanks
Upvotes: 0
Views: 614
Reputation: 440
try this:
<script>
$(document).ready(function() {
$("input[id$='Txt_Zip']").on('keypress', function(e) {
var code = (e.keyCode ? e.keyCode : e.which);
if(code == 13) {
$("input[id$='Txt_Second').val($(this).val());
return false;
}
});
});
</script>
Upvotes: 1
Reputation: 11149
Just add e.preventDefault();
inside the if statement braces. This tells the event to not carry out the default action (submitting the form).
function runScript(e)
{
if (e.which == 13 || e.keyCode == 13)
{
var zip =document.getElementById("txtZip").value;
document.getElementById("txt_Second").value=zip;
if (e.preventDefault) {
e.preventDefault();
}
return false;
}
}
Upvotes: 0
Reputation: 15934
Try this:
function runScript(e)
{
if (e.which == 13 || e.keyCode == 13)
{
var zip =document.getElementById("txtZip").value;
document.getElementById("txt_Second").value=zip;
return false;
}
}
Upvotes: 0