[Javascript] Select all text from a textarea doesn't work

Here we discuss all topic related to PHPBB 3.0 and up

[Javascript] Select all text from a textarea doesn't work

Postby admin » Wed May 31, 2017 6:12 pm

This is a fix for the select all text from a textarea BBCode, which either fails to select or jumps to the top of the page after clicking on the "select all" link. A problem on phpBB 3.0.xx forums which has developed with all modern browsers. Just do this in your template style if not running Prosilver.

Go to /styles/prosilver/template/forum_fn.js:
FIND LINE (259):
Code: Select all
function selectCode(a)
{
   // Get ID of code block
   var e = a.parentNode.parentNode.getElementsByTagName('CODE')[0];

   // Not IE and IE9+
   if (window.getSelection)
   {
      var s = window.getSelection();
      // Safari
      if (s.setBaseAndExtent)
      {
         s.setBaseAndExtent(e, 0, e, e.innerText.length - 1);
      }
      // Firefox and Opera
      else
      {
         // workaround for bug # 42885
         if (window.opera && e.innerHTML.substring(e.innerHTML.length - 4) == '<BR>')
         {
            e.innerHTML = e.innerHTML + '&nbsp;';
         }

         var r = document.createRange();
         r.selectNodeContents(e);
         s.removeAllRanges();
         s.addRange(r);
      }
   }
   // Some older browsers
   else if (document.getSelection)
   {
      var s = document.getSelection();
      var r = document.createRange();
      r.selectNodeContents(e);
      s.removeAllRanges();
      s.addRange(r);
   }
   // IE
   else if (document.selection)
   {
      var r = document.body.createTextRange();
      r.moveToElementText(e);
      r.select();
   }
}


REPLACE WITH:
Code: Select all
function selectCode(a) {
   'use strict';

   // Get ID of code block
   var e = a.parentNode.parentNode.getElementsByTagName('CODE')[0];
   var s, r;

   // Not IE and IE9+
   if (window.getSelection) {
      s = window.getSelection();
      // Safari and Chrome
      if (s.setBaseAndExtent) {
         var l = (e.innerText.length > 1) ? e.innerText.length - 1 : 1;
         try {
            s.setBaseAndExtent(e, 0, e, l);
         } catch (error) {
            r = document.createRange();
            r.selectNodeContents(e);
            s.removeAllRanges();
            s.addRange(r);
         }
      }
      // Firefox and Opera
      else {
         // workaround for bug # 42885
         if (window.opera && e.innerHTML.substring(e.innerHTML.length - 4) === '<BR>') {
            e.innerHTML = e.innerHTML + '&nbsp;';
         }

         r = document.createRange();
         r.selectNodeContents(e);
         s.removeAllRanges();
         s.addRange(r);
      }
   }
   // Some older browsers
   else if (document.getSelection) {
      s = document.getSelection();
      r = document.createRange();
      r.selectNodeContents(e);
      s.removeAllRanges();
      s.addRange(r);
   }
   // IE
   else if (document.selection) {
      r = document.body.createTextRange();
      r.moveToElementText(e);
      r.select();
   }
}


Go to ACP and refresh templates.
Go to ACP Index to purge cache.
Done. Problem solved!

This code turns the 'select all' bbcode link into a minimizer (the text/code in the textarea is converted to text/html without separation) Example: Ax Bx Cx = AxBxCx. Ideal code for selecting html that you wish your users copy and paste:

Go to /styles/prosilver/template/forum_fn.js
Paste the following code at the top of the file:

This code is hidden: 

To view this code please make a small donation or login to see it!
The AVNW Support Team
http://www.AVeryNiceWeb.com

If there is a false accusation made against you, then remove this fake report.

Shopping for a new website? See our design deals.

How much is your website worth? Get a free estimate.

And, if our post has provided you with great information, there are a few ways you can show your appreciation:
  1. Like us on Facebook
  2. Post a review.- Posting a good review about this site could help us provide improved support.

Thank You!
User avatar
admin
Site Admin
 
Posts: 561
Joined: Sun Jan 12, 2014 11:18 pm
Location: New York City

Return to PhpBB 3.0.XX

Who is online

Users browsing this forum: No registered users and 1 guest

cron