April 25, 2024, 01:27:00 AM
Forum Rules: Read This Before Posting


Topic: SurroundText  (Read 12618 times)

0 Members and 1 Guest are viewing this topic.

Offline enahs

  • 16-92-15-68 32-7-53-92-16
  • Retired Staff
  • Sr. Member
  • *
  • Posts: 2179
  • Mole Snacks: +206/-44
  • Gender: Male
SurroundText
« on: December 01, 2007, 06:28:14 PM »
Hey Mitch,
Would you perhaps be willing to share the "surroundText" function used to surround the highlighted text with message board code? I made my own, but it does not seem to play well with all Browsers across all OS's :(.
I would think this would be easily available sample code to find on the many coding sites, but for the life of me I can just not seem to find it (probably just can not find the correct keywords to search by).




Offline Mitch

  • General Chemist
  • Administrator
  • Sr. Member
  • *
  • Posts: 5298
  • Mole Snacks: +376/-3
  • Gender: Male
  • "I bring you peace." -Mr. Burns
    • Chemistry Blog
Re: SurroundText
« Reply #1 on: December 02, 2007, 02:13:04 AM »
huh?
Most Common Suggestions I Make on the Forums.
1. Start by writing a balanced chemical equation.
2. Don't confuse thermodynamic stability with chemical reactivity.
3. Forum Supports LaTex

Offline Borek

  • Mr. pH
  • Administrator
  • Deity Member
  • *
  • Posts: 27662
  • Mole Snacks: +1801/-410
  • Gender: Male
  • I am known to be occasionally wrong.
    • Chembuddy
Re: SurroundText
« Reply #2 on: December 02, 2007, 04:07:30 AM »
http://www.chemicalforums.com/Themes/default/script.js


// Surrounds the selected text with text1 and text2.
function surroundText(text1, text2, textarea)
{
   // Can a text range be created?
   if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange)
   {
      var caretPos = textarea.caretPos, temp_length = caretPos.text.length;

      caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2;

      if (temp_length == 0)
      {
         caretPos.moveStart("character", -text2.length);
         caretPos.moveEnd("character", -text2.length);
         caretPos.select();
      }
      else
         textarea.focus(caretPos);
   }
   // Mozilla text range wrap.
   else if (typeof(textarea.selectionStart) != "undefined")
   {
      var begin = textarea.value.substr(0, textarea.selectionStart);
      var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);
      var end = textarea.value.substr(textarea.selectionEnd);
      var newCursorPos = textarea.selectionStart;
      var scrollPos = textarea.scrollTop;

      textarea.value = begin + text1 + selection + text2 + end;

      if (textarea.setSelectionRange)
      {
         if (selection.length == 0)
            textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
         else
            textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
         textarea.focus();
      }
      textarea.scrollTop = scrollPos;
   }
   // Just put them on the end, then.
   else
   {
      textarea.value += text1 + text2;
      textarea.focus(textarea.value.length - 1);
   }
}


Can't say it was hard to find. Please read forum rules ;D
ChemBuddy chemical calculators - stoichiometry, pH, concentration, buffer preparation, titrations.info

Offline enahs

  • 16-92-15-68 32-7-53-92-16
  • Retired Staff
  • Sr. Member
  • *
  • Posts: 2179
  • Mole Snacks: +206/-44
  • Gender: Male
Re: SurroundText
« Reply #3 on: December 02, 2007, 10:03:06 AM »
Thanks Borek! And  :P to form rules! I looked through the page source but did not see the function. I did not see that all the functions were listed in a separate file! To be honest, who wanted to look through all the sources? I just did a search of the source!


Offline Mitch

  • General Chemist
  • Administrator
  • Sr. Member
  • *
  • Posts: 5298
  • Mole Snacks: +376/-3
  • Gender: Male
  • "I bring you peace." -Mr. Burns
    • Chemistry Blog
Re: SurroundText
« Reply #4 on: December 02, 2007, 01:29:03 PM »
I still don't know what you guys are talking about. But if your both satisfied, I'm satisfied. :)
Most Common Suggestions I Make on the Forums.
1. Start by writing a balanced chemical equation.
2. Don't confuse thermodynamic stability with chemical reactivity.
3. Forum Supports LaTex

Offline enahs

  • 16-92-15-68 32-7-53-92-16
  • Retired Staff
  • Sr. Member
  • *
  • Posts: 2179
  • Mole Snacks: +206/-44
  • Gender: Male
Re: SurroundText
« Reply #5 on: December 02, 2007, 03:51:47 PM »
You know when you are typing a message, like where I am typing right now. You use the mouse to highlight the text, you then click say the "bold" button above the text area, it automatically places the command for the bold around the text.

That is what we are talking about.

Offline Mitch

  • General Chemist
  • Administrator
  • Sr. Member
  • *
  • Posts: 5298
  • Mole Snacks: +376/-3
  • Gender: Male
  • "I bring you peace." -Mr. Burns
    • Chemistry Blog
Re: SurroundText
« Reply #6 on: December 02, 2007, 08:41:45 PM »
Oh, ok. Its all based on the old UBB code. So, I have those things well memorized.
Most Common Suggestions I Make on the Forums.
1. Start by writing a balanced chemical equation.
2. Don't confuse thermodynamic stability with chemical reactivity.
3. Forum Supports LaTex

Sponsored Links