Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here is my code when i type d in textarea it replaces "d" as "document" then i want the cursor at end of the text but it shows at 1st position how to fix it? i m using ucbrowser 11.0 browser.

What I have tried:

<html>

<body>

<textarea id="ta" ></textarea>

</body>

<script>

function replacetext()
{

var ta = document.getElementById("ta");

var a = ta.value.replace("d","document");

ta.value = a;

ta.selectionStart = ta.selectionEnd = ta.value.length;

}

</script>

</html>
Posted
Updated 13-Sep-16 22:42pm
Comments
prasad sawant 12-Sep-16 5:16am    
in textarea element i have an attribute [ oninput= "replacetext()" ] this website may be doesnt allow some attributes so now suggest me answer.

oninput is not a right choice since it will change for every key press.
use onchange instead , however you have to validate the letter 'd' which might contain in any word of the input.
you have to use /d/g for global replacement.

HTML
<html>
<body>

    <textarea id="ta" onchange="replacetext()"></textarea>
    <script>

        function replacetext() {
            var ta = document.getElementById("ta");
            ta.value = ta.value.replace(/d/g, "document"); 
            ta.selectionStart = ta.selectionEnd = ta.value.length;

        }

    </script>
</body>

</html>
 
Share this answer
 
Comments
prasad sawant 13-Sep-16 3:29am    
hey its not replacing "d" as "document" its only change when i click not on textarea but other area. i want cursor at end
Karthik_Mahalingam 13-Sep-16 3:55am    
ok.
Karthik_Mahalingam 13-Sep-16 3:55am    
what about the previous text and all
prasad sawant 14-Sep-16 4:29am    
when i use oninput it directly replace words but cursor blinks between d and o but i want it at end, when i use onchange it doesnt replace words but when i click on other side without touching textarea its replace words but doesnt place cursor anywhere, i think oninput is perfect event, but some advance coding will need
Karthik_Mahalingam 14-Sep-16 8:23am    
ok
after many tries i tried this code and its work perfect











function replacetext()
{

var ta = document.getElementById("ta");

var replaced = ta.value.replace(/d/g,"document");

ta.value = "";

ta.value = replaced;

ta.selectionStart=ta.selectionEnd=replaced.length;

}



 
Share this answer
 
Comments
Maciej Los 14-Sep-16 4:47am    
This is not an answer! This is an exact copy of solution 1!

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