Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2014-06-20 15:09:53

jonathanbrickman0000
New Member
From: Topeka, Kansas, USA
Registered: 2011-01-22
Posts: 5
Website

Highlight paragraph on click?

How do I set up to automatically highlight any paragraph or text area of particular types (a .pre or .bc) on click?

Last edited by jonathanbrickman0000 (2014-06-20 15:13:03)

Offline

#2 2014-06-20 17:28:20

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Highlight paragraph on click?

You will have to use feature detection and non-standard APIs to get something selected. Along the lines of:

// Trigger the handler when 'pre' is clicked.

$('pre').on('click', function() {
    // Get raw Element object of the target jQuery object and define needed variables.

    var text = $(this).get(0), range, selection;

    // Check if the browser is Internet Explorer.

    if (window.document.body.createTextRange) {
        // If so, selected the element's content.
        range = window.document.body.createTextRange();
        range.moveToElementText(text);
        range.select();
    } else if (window.getSelection) {
        // Any other browser than IE.
        selection = window.getSelection();
        range = window.document.createRange();
        range.selectNodeContents(text);
        selection.removeAllRanges();
        selection.addRange(range);
    }
});

The above uses jQuery for event handling.

Offline

Board footer

Powered by FluxBB