Press here to unveil and modify using the Redactor JS WYSIWYG HTML editor

I am working on a project where I have three separate divs that are set to be editable using the attribute contenteditable="true". Additionally, I have configured the redactor wysiwyg toolbar to remain fixed on the page.

Here is the structure in HTML:

<div id="toolbar_wrapper">
  <div id="toolbar">
  </div>
</div>

<div id="content">
  <div class="redactor redactor1" contenteditable="true">
    <h1>I am a Header</h1>
    <p>I am a paragraph.</p>
  </div>
  <div class="redactor redactor2" contenteditable="true">
    <h1>I am a Header</h1>
    <p>I am a paragraph.</p>
  </div>
  <div class="redactor redactor3" contenteditable="true">
    <h1>I am a Header</h1>
    <p>I am a paragraph.</p>
  </div>

For styling, here is the CSS snippet:

#toolbar {
  position: fixed;
  z-index: 10;
}

My challenge now is to make the redactor toolbar appear only when one of the divs (redactor1, redactor2, or redactor3) is clicked, and hide it when clicked outside those specific divs. Can anyone provide guidance on how to achieve this? Thank you.

You can view a sample code snippet on CodePen.

Answer №1

insert the following code snippet into your JavaScript file

$(function()
{
    $('.redactor1').redactor({
        focus: true,
        toolbarExternal: '#toolbar'
    });

});

$(document).click(function(){
    $("#toolbar_wrapper").hide();
});
$("#toolbar_wrapper").click(function(e){
    e.stopPropagation();
  $("#toolbar_wrapper").css('display','block');
});
$(".redactor").click(function(e){
  e.stopPropagation();
    $("#toolbar_wrapper").css('display','block');
});

Answer №2

If you want to customize the toolbar for each instance of redactor, follow these steps:

  <div id="toolbar_wrapper">
    <div class='toolbar' id="toolbar_for_1"></div>
    <div class='toolbar' id="toolbar_for_2"></div>
    <div class='toolbar' id="toolbar_for_3"></div>
  </div>

Next, make sure to activate the appropriate toolbar whenever a specific redactor is in focus:

$(function() {
  $.each($('.redactor'), function(i){
    var toolbar_id = '#toolbar_for_' + (i + 1);
    $(this).redactor({
      focus: true,
      toolbarExternal: toolbar_id,
      focusCallback: function(e){
        $(".toolbar").hide();
        $(toolbar_id).show();
      }
    });
  });
})

Take a look at this live example here

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be causing the page to automatically scroll to the bottom upon loading?

My experience with this bootstrap/jquery page in Firefox (v39, latest updates installed) has been unusual as it always seems to jump to the bottom of the page upon clicking. Upon inspecting the code, I couldn't find any JavaScript responsible for thi ...

When making an Ajax request to another website, the response received is in HTML format instead of

I am attempting to retrieve an array by making an AJAX GET request to a URL. Below is my controller code, which is hosted locally at localhost:3000 def merchant_ids merchants = Merchant.where(id: params[:id]).pluck(:merchant_name, :id, :merchant_city, ...

Having trouble with $(document).ready not functioning correctly?

After following the instructions to move my jQuery CDN to the bottom of the page, I encountered a problem. The script below was functioning perfectly when my jquery files were at the top of the page: if ($(window).width() >= 768) { $('.col-l ...

HTML 4.01 character and character offset attributes

I'm curious about the purpose of the charoff attribute in HTML 4.01 Transitional. Specifically, consider a table column that contains the following cells: <td>1.30</td> <td>10</td> <td>100.2</td> <td>1000 ...

What could be the reason for the malfunctioning of this JavaScript code?

Regarding the content found in this post: How to display a loading image while the actual image is downloading. I have implemented the following code snippet, however, I am facing an issue where the #loader_img element does not hide. Additionally, I would ...

Vue JS nested component does not appear in the document object model

I developed two separate VueJS components - one displaying a modal and the other featuring HTML input fields. When I attempt to nest these two components, the inner component fails to appear in the DOM. What could be causing this issue? Here's a snip ...

Avoid saving sessions in Node.js using Express.js and Mongoose

I've recently embarked on learning node.js and have set up a Phonegap project. However, I'm facing an issue where req.session is coming up as undefined in my code: app.js const express = require('express'), routes = require('./rou ...

Aligning the Navigation Bar to the Upper Right Corner

I'm attempting to rearrange my Nav Bar to be at the Top Right, with my logo on the Top Left all in one line. Unfortunately, I'm struggling to achieve this and could really use some guidance. As a newcomer to HTML and CSS, I find it challenging to ...

Is there a way for me to choose all the classNames that conclude with a specific word within the MUI sx property?

I am currently working with MUI and I have a need to modify certain properties that are prefixed with random IDs. How can I target, for instance, the first one using: '& endsWith(MuiAccordionDetails-root)' I wish to achieve this because the ...

Exploring the Relative Positioning of Two div Elements

Can anyone assist me with finding and comparing the positions of two '<div>' tags using an if statement? I stumbled upon a suggestion in my research, which goes like this: var obstacle = $('#obstacle').css('left', &apo ...

Include CakePHP named parameters in the URL when selecting from a list

If I have a selection list like the one below: <select name='languages'> <option value='german'>German</option> <option value='english'>English</option> </select> How can I use Jav ...

Having trouble with Angular2's Maximum Call Stack Exceeded error when trying to display images?

I am facing an issue in my Angular2 application where I am trying to display an image in Uint8Array format. The problem arises when the image size exceeds ~300Kb, resulting in a 'Maximum Call Stack Exceeded' error. Currently, I have been able to ...

Guide on transferring JSON data to a PHP script by utilizing jQuery's submit function

Hey there, I am just starting to explore JSON in jQuery. I have created an HTML form using JSON data and JavaScript, where the JSON string holds the result. My question is how do I send this string to a PHP file? I am currently using an ajax function to ...

At times, the status code 0 and ready state 0 is returned by Ajax file uploads

After carefully reviewing the following thread: jQuery Ajax - Status Code 0? I was unable to find a definitive solution to my issue. Therefore, I am reaching out here in hopes that someone can guide me in the right direction. In my code, I have an Angul ...

Tips for applying stroke and shadow effects specifically when the mouse is moving over a canvas:

How can we create a shadow and stroke effect for circles drawn using HTML canvas only during mouse hover? I have two circles and would like to add a shadow and stroke effect when the mouse hovers over them. However, the issue is that once the mouse moves ...

When updating the innerHTML attribute with a new value, what type of performance enhancements are implemented?

Looking to optimize updating the content of a DOM element called #mywriting, which contains a large HTML subtree with multiple paragraph elements. The goal is to update only small portions of the content regularly, while leaving the majority unchanged. Co ...

issue with retrieving HTML data from PHP and converting it to JSON

I've tried multiple search queries but nothing seems to be working. Can someone please assist me? ----------jQuery Function ---------------------------- function showRequestFunctionToBox(thisId){ var encodedItemId = thisId; ...

Is it possible to trigger AJAX calls in ASP.NET MVC after the page has finished loading?

On my webpage, I have 6 different charts displayed. I am looking for a way to optimize the loading time by initially loading just the layout of the page and then fetching each chart separately with AJAX. Since it takes some time for each chart to generat ...

Is it possible to make the dashboard reach 100% height?

I am struggling with designing a dashboard that needs to take up 100% of the height of any monitor. Despite reading multiple posts on Stackoverflow and trying different options like hv 100% and height: 100%, I still can't get it to work. I am using Bo ...

Switching images with Jquery

Seeking help to create an FAQ page using HTML, CSS, and jQuery. My goal is to rotate an arrow when a question is opened and revert it back when another question is clicked. I've successfully achieved this with <p> tags but facing issues with the ...