/    Sign up×
Community /Pin to ProfileBookmark

Is there a way to cut and repaste text in a text area? I want to be able to click on a button which will cut the selected text and re-paste it with an additional word attached to it. For example if the word “Hello” was selected and you clicked on a button, it would cut the Hello and re-paste it as “Helloxxx”.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@toicontienDec 27.2007 — There is a way. Web browsers, save for Internet Explorer, do not have access to the clip board of your computer (the area used to "copy" and "cut" data). To get around this, you'd need a hidden form field to act as the clip board.
[code=html]<textarea></textarea>

<input type="hidden" id="clipboard" value="">[/code]


  • 1. Copy the selected text from the TEXTAREA to the hidden INPUT.


  • 2. Optionally, remove the selected text from the TEXTAREA.


  • 3. Append the new word to the text in the hidden INPUT


  • 4. Insert the text in the hidden INPUT into the TEXTAREA at the cursor. If a block of text is selected, the new text gets inserted in place of the selected text.


  • Note that Internet Explorer implements text selections vastly different than all other browsers.

    You also don't really need a hidden form field. You could create a global JavaScript variable to hold the "clipboard" text too.
    ×

    Success!

    Help @malikah spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.5,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...