Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
Javascript to insert emoji icons to textarea when click not working

I want to make a script to insert emojis into textarea when click. But it never works.

Can anybody point me out where am I doing wrong?

Thank you.

<form>
    <button value="๐Ÿ˜€">๐Ÿ˜€</button>
    <button value="๐Ÿ˜‚">๐Ÿ˜‚</button>
    <button value="๐Ÿ‘">๐Ÿ‘</button>
    <br>
    <textarea id="text1" cols="40" rows="3" placeholder="This is some dummy text."></textarea>

</form>

<script type="text/javascript ">
    let currentInput = document.getElementById('text1');

    function insertAtCursor() {
        let cursorPos = currentInput.selectionStart;
        let v = currentInput.value;
        let textBefore = v.substring(0, cursorPos);
        let textAfter = v.substring(cursorPos, v.length);
        currentInput.value = textBefore + this.value + textAfter;

        cursorPos += this.value.length;
        currentInput.focus();
        currentInput.setSelectionRange(cursorPos, cursorPos);
    };


    if (document.querySelector('button')) {
        document.querySelectorAll('button').forEach((elem, i) => {
            elem.addEventListener("click", function(event) {

                insertAtCursor();

            });
        });
    }
</script>
about 4 years ago ยท Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!