/    Sign up×
Community /Pin to ProfileBookmark

Text not adding to text area correctly

Hi,

I have the following javascript in the header of my page to allow me to add text to a text area on a button click:

[CODE]function insert(el,ins) {
if (el.setSelectionRange){
el.value = el.value.substring(0,el.selectionStart) + ins + el.value.substring(el.selectionStart,el.selectionEnd) + el.value.substring(el.selectionEnd,el.value.length);
}
else if (document.selection && document.selection.createRange) {
el.focus();
var range = document.selection.createRange();
range.text = ins + range.text;
}
}[/CODE]

And then I have some buttons on my page like this:

<input name=”bold” type=”button” id=”bold” onclick=”insert(this.form.replytext,'[ b][ /b]’)” value=”Bold”>
<input name=”italic” type=”button” id=”italic” onclick=”insert(this.form.replytext,'[ i][ /i]’)” value=”Italic”>

The first button works perfectly, however the second button (italic) and any additional buttons add their code, however they always also insert the [ b][ /b] code after it.

So for example, clicking the italic button would insert [ i][ /i][ b][ /b] into the text area. (I have added spaces so that the code shows up in this message).

Can anyone help?

Thanks

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@toicontienMay 07.2008 — This is a frustrating issue. Every browser interprets where the cursor should go after inserting text differently. Firefox puts the cursor after the selection. I think Internet Explorer also does. Safari and Opera, I believe, put the cursor [I]before[/I] the inserted text. You must manually set the cursor position after inserting the text.

  • 1. Keep track of how many characters long the opening and closing BB Code tags are.


  • 2. If the cursor position didn't change, move the cursor ahead as many characters as the opening BB Code tag is wide.


  • 3. If the cursor position did change, move the cursor back as many characters as the closing BB Code tag is wide.


  • 4. For Internet Explorer, I haven't figured this out yet. I wrote the BB Code editor on www.motortopia.com and I still haven't fully sorted IE-Win's eccentricities out. I'm also working on another code editor for browsers and I'm trying to figure out how to move the cursor with Internet Explorer, and I've failed miserably so far.
  • ×

    Success!

    Help @grandad80 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.25,
    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: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

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