Syed Raza
Syed Raza

Reputation: 331

On Pressing Enter Key It Move to Other Page Rather Then Calling Java Script (Code Attached)

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

Answers (3)

UmaKiran
UmaKiran

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

Nathan
Nathan

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

webnoob
webnoob

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

Related Questions