• I would like to add automatically a class to an anchor.

    Basically when you want to add an anchor the code is :<a id="XXX"></a>

    But I would like to see :<a id="XXX" class="YYYYYYY"></a>

    So I have seen this file : tinymce-advanced/mce/anchor/plugin.js
    And I have tried to change this code :

    if (isAnchor) {
                  selectedNode.removeAttribute('name');
                  selectedNode.id = id;
                } else {
                  editor.selection.collapse(true);
                  editor.execCommand('mceInsertContent', false, editor.dom.createHTML('a', {
                    id: id,
                  }));
                }

    IN :

    if (isAnchor) {
                  selectedNode.removeAttribute('name');
                  selectedNode.id = id;
    			  selectedNode.setAttribute('class','ancre');
                } else {
                  editor.selection.collapse(true);
                  editor.execCommand('mceInsertContent', false, editor.dom.createHTML('a', {
                    id: id,
                  }));
                }

    But in vain

    I have tried with only
    selectedNode.createAttribute('class'); or selectedNode.className = 'test';

    But no…

    I need help please !!
    Thank you so much.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Andrew Ozz

    (@azaozz)

    Hmm, you can modify the anchor script, but it will be overwritten next time you update the TinyMCE Advanced plugin.

    A better option would be to copy that file, add it from a WP plugin, then edit it do always add the class. The proper JS for there would be:

    
    if (isAnchor) {
        // When editing existing anchors.
        selectedNode.removeAttribute('name');
        selectedNode.id = id;
        selectedNode.setAttribute('class','ancre');
     } else {
         // When adding new.
         editor.selection.collapse(true);
         editor.execCommand('mceInsertContent', false, editor.dom.createHTML('a', {
                    id: id,
                    'class': 'my-class-name'
          }));
    }
    
    Thread Starter hopseb

    (@hopseb)

    Hello
    Thank you for your help but it doesn’t work.
    I have editted the anchor script in order to test.

    I still have the the anchor whithout the class.
    And how can I add something between tags ? Like :

    <a href="#XXXX" class="my-class-name">mytext</a>

    Thank you so much for your help !
    Best regards,
    Sebastien.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Add class on anchor’ is closed to new replies.