1

Possible Duplicate:
Prevent form submission with enter key

In my project, I am trying to disable the Enter key and give my own enter key function.(which I havent done)

The below function is working but whenever I press Enter key inside the TextBox field, the content is adding to div (as needed) and also in the TextBox field, enter key function is happening (which I dont want). How to stop the enter Key function to run on the TextBox field? For clear understanding please see my comments in the below code.

.aspx file (working)

<asp:TextBox ID="msg" BackColor="Transparent" runat="server" BorderStyle="None"
                        TextMode="MultiLine" />

jQuery (working)

$('#msg').keypress(function (e) {
            if (e.which == 13) {

                //Shows the TextBox text in a Div, which I want to.
                chat.server.send($('#msg').val());  //also going one step down in TextBox field which I dont want to.

                $('#msg').val(''); //Clearing the Text in TextBox field

                //what should I add here to make the Enter Key work only for the DIV?
            }
        });

source

Community
  • 1
  • 1
Mr_Green
  • 40,727
  • 45
  • 159
  • 271
  • Yes, and see the duplicate question you've asked, just as a million other people have... – Ian Nov 26 '12 at 07:48
  • @Ian What good would `close` do after getting a good answer? :) – Mr_Green Nov 26 '12 at 07:51
  • You should just check more carefully before you post a question. – Abijeet Patro Nov 26 '12 at 10:44
  • @AbijeetPatro I searched alot before asking here. I dont know the keyword with what the [**`duplicate question`**](http://meta.stackexchange.com/a/115639/196352) is.. BTW, Stack overflow is made for having Q & A's. and we should show research in our posts which I have done as you can see clearly. – Mr_Green Nov 26 '12 at 12:07

2 Answers2

4

try e.preventDefault() like this

$('#msg').keypress(function (e) {
            if (e.which == 13) {
                e.preventDefault();
                //Shows the TextBox text in a Div, which I want to.
                chat.server.send($('#msg').val());  //also going one step down in TextBox field which I dont want to.

                $('#msg').val(''); //Clearing the Text in TextBox field

                //what should I add here to make the Enter Key work only for the DIV?
            }
        });
rahul
  • 7,573
  • 7
  • 39
  • 53
0

Also try using C# for validating in C# also. Just write the below function and call the function supplying parameter as this(have mentioned below)

public void disable_TextBox_Enter(Control parent)
    {
        foreach (Control c in parent.Controls)
        {
            if ((c.Controls.Count > 0))
            {
                disable_TextBox_Enter(c);
            }
            else
            {
                if (c is TextBox)
                {
                    ((TextBox)(c)).Attributes.Add("onkeydown", "return (event.keyCode!=13);");

                }
                if (c is GridView)
                {

                    ((GridView)(c)).Attributes.Add("onkeydown", "return (event.keyCode!=13);");

                }

            }
        }
    }

U can give call to the function like this :

disable_TextBox_Enter(this);