简体   繁体   中英

Get id of element within ckeditor

Unfortunately I have to open a new question for this as I cannot yet add comments to replies (in foreign posts).

In this question ( Clicking CKEditor content to show alert ) Palec posted a solution to get onclick events within CKEditor that works very well for me but as I hardly know jQuery I would need to know how I can get the id of the element (in this case the id of a div) within CKEditor using the code below.

This is the piece of code that I'm talking about.

CKEDITOR.on('instanceReady', function() {
    $('.cke_contents iframe').contents().click(function() {
        alert('Clicked!');
    });
});

Thanks for your help, Pascal

You can get the id of an element in native Javascript using

element.id

and in jQuery using

$('element').attr('id')

Working Example:

 // Native Javascript var myDiv = document.getElementsByTagName('div')[0]; var myDivId = myDiv.id; window.alert('Native javascript has found the id of the <div> is "' + myDivId + '"'); // jQuery $(document).ready(function(){ alert('jQuery Library has found the id of the <div> is "' + $('div').attr('id') + '"'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="my-div"></div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM