Issue #1669💬 AnsweredOpened December 18, 2018by SeongwoonHong0 reactions

Custom link modal doesn't update the current text but add another text with link

快速解答by SeongwoonHong

I'd like to add one more, inside onclick handler for btnLink, <a class="link" href="${inputEl.value}">${rte.selection()}</a> returns <a class="link" href="..."></a> the inner text is empty.. i have no idea how to approach this

Read full answer below ↓

Question

Hello Artur,

I'm trying to have a custom modal when clicked link button in RTE. but it doesn't convert selected text to link but what it does is it adds another text with link.

      this.editor.RichTextEditor.add('custom-link', {
        icon: '<img src="assets/images/link.png" />',
        attributes: {title: 'Link', class: 'gjs-rte-action'},
        result: rte => {
          // define what's going inside modal
          const customLinkContainer = document.createElement('div');
          const btnLink = document.createElement('button');
          const linkInputEl = document.createElement('input');
          linkInputEl.setAttribute('id', 'custom-link-input');
          linkInputEl.value = `<a class="link" href="asdf">${rte.selection()}</a>`
          btnLink.innerHTML = 'aa';
          btnLink.onclick = () => {
            const inputEl = document.getElementById('custom-link-input');
            const selectedEl = this.rte.selection();
            rte.insertHTML(`<a class="link" href="${inputEl.value}">${rte.selection()}</a>`);
            modal.close();
          };
          customLinkContainer.appendChild(linkInputEl);
          customLinkContainer.appendChild(btnLink);

         // configure modal
          modal.setTitle(t('Link'));
          modal.setContent(customLinkContainer);
          modal.open();
        }
      });

Thank you..

Answers (3)

SeongwoonHongDecember 18, 2018

I'd like to add one more, inside onclick handler for btnLink, <a class="link" href="${inputEl.value}">${rte.selection()}</a> returns <a class="link" href="..."></a> the inner text is empty.. i have no idea how to approach this

lock[bot]December 20, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Free option

Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.

Browse free plugins →
Premium option

Premium plugins ship with support, regular updates, and production-ready features — save days of integration work.

Browse premium plugins →

Related tutorials

In-depth guides on the same topic.

All tutorials →

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.