简体   繁体   中英

How to Insert dot(.) again in textarea, if we have created a function to replace it?

I have created a function to replace dot(.) into purnviraam, it works fine. but If we need to insert ". " again in textarea, I have created a button neart textarea with function. it inserts dot"." but pressing shift, it also converts in purnviraam. Please guide me, I don't have much knowledge of js coding.

 $(function() { $('textarea').on("keyup", function(e) { var val = $(this).val(); var str = val.replace('.', String.fromCharCode(2404)); $(this).val(str); }); }); function myFunction1() { insertfullstop.data.value += '.'; document.getElementById("data").focus(); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tr> <td colspan="1"> <form id="insertfullstop"> <textarea type='text' id="data" class="js-copytextarea" style="width: 100%; max-width: 400px; height: 200px; border-radius: 6px 6px 6px 6px;" name="data" autofocus="" placeholder="Start Type Here................"></textarea> </form> </td> </tr> <tr align="right"> <td style="padding-top: 5px; float='center' padding-left: 1px; " colspan="1"> <strong style="font-size: 16px;"></strong> <button id="fullstop" OnClick="myFunction1()" class="btn1" title="Full Stop" type="button">.</button> </td> </tr> </table>

The problem is that you replace. with | on EVERY keyup event, which means that; you can add the dot with your function correctly but when you type the textarea it is going to replace the previously added dot with |

To solve this: check if the typed character is a dot or not. If it is a dot, only then, replace it with a |. This way, it will not replace the previously added dot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM