Click here to Skip to main content
15,887,328 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello friends,
I was trying to write javascript function, where in I check whether user has pressed enter key. If so then through javascript i want to add * in new line in textbox

here is my aspx code:
<asp:TextBox TextMode="MultiLine" runat="server" Width="80%" Height="100px" ID="txtComments" onkeypress="appendText(event);" Text="* ">
</asp:TextBox>


Here is my javascript code:
function appendText(e) {
            var charChode;
            if (e && e.which) {
                e = e;
                charChode = e.which;
            }
            else {
                e = event;
                charChode = e.keyCode;
            }
            if (charChode == 13) {
                var TheTextBox = document.getElementById("<%=txtComments.ClientID %>");
//                var textValue = document.getElementById("<%=txtComments.ClientID %>").value;
//                textValue = textValue.replace(/\n\r?/g, '<br />* ');
                TheTextBox.value = TheTextBox.value + "<br/> * ";
                return true;
            }
            else {
                return false;
            }



In short:
I want to create bulleted text box, as user press enter bullet should appear on next line

Any solution??

Thanks in advance
Posted
Comments
Mario Majčica 1-Jun-12 3:45am    
What is the problem with the current code? When managing Enter Key do not forget to prevent the default event and cancel bubble.
dhage.prashant01 1-Jun-12 5:00am    
no idea
can you give some idea about it?

replace
C#
TheTextBox.value = TheTextBox.value + "<br /> * ";


to

C#
TheTextBox.value = TheTextBox.value + "\n * ";
 
Share this answer
 
v2
Comments
dhage.prashant01 1-Jun-12 4:03am    
I tired
Its working fine but the output goes this way when i click enter
* asd
*
<- here is where cursor goes

i want cursor next to *

any idea?
here is what i tried and was successful

function appendText(e) {
            var charChode;
            if (e && e.which) {
                e = e;
                charChode = e.which;
            }
            else {
                e = event;
                charChode = e.keyCode;
            }
            if (charChode == 13) {
                var TheTextBox = document.getElementById("<%=txtComments.ClientID %>");
                if (window.ActiveXObject) {
                    e.returnValue = false;
                    e.cancel = true;
                    TheTextBox.value = TheTextBox.value + "\n* ";
                }
                else {
                    e.preventDefault();
                }     
            }


Hope it helps you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900