Some words in my tinyMCE textarea editor are in a span tag with a specific class called "myclass". For instance, the word Hello that is visible in the tinyMCE textarea editor is in the source code with the following HTML code:
<span class="myclass" id="hello">Hello</span>
I try to launch a function on double click on the word Hello.
The usual jQuery code does not work for words inside the tinyMCE editor:
$(document).ready(function() {
$('.myclass').dblclick(function() {
alert('class found');
});
});
The function does not fire when I double click on the word Hello that is in the editor.
How can I bind the function to the tinyMCE editor?
TinyMCE uses an iframe element, so you cannot use $('.myclass') on your "main" scope in order to get elements inside that iframe (The content of the iframe is a different scope).
Instead - you need to run $('.myclass').dblclick in the scope of that iframe.
To do so you can use the setup callback and the editor.on("init" event that TinyMCE gives you:
tinymce.init({
selector:'textarea',
setup: function(editor) {
editor.on("init", function(){
editor.$('p').on('dblclick', function() {
alert('double clicked');
});
});
}
});
Live demo here.
Note that editor.$ is not a jQuery object, so you cannot do everything you are used to with jQuery, however it's pretty close.
Related
I'm trying to trigger click event on tag inside the div tag.here is my code
$(document).ready(function () {
$("#span").on('click',function(e){
console.log("clicked in link");
});
});
and here is the html structure (this is a PDF-tron Pdf viewer)
but , it doesn't work. How could I trigger the click event using pure Java script?
Thank you.
The issue is that span is a tag name, not an ID, so instead of $("#span") you should do $("span"), but be careful, there might be other span elements there as well.
"How could I trigger the click event using pure Java script?"
The trick for PDFTron WebViewer is that it reners the document in an iFrame.
So, to access the iframe DOM element, you can do this in the WebViewer constructor, for example:
Webviewer(
{
/// ...
},
document.getElementById('viewer')
).then((instance) => {
instance.iframeWindow.document.querySelector('put_your_selector_here').addEventListener('click', () => {
console.log('clicked');
});
});
Use jQuery.click:
$("span.link").click()
#Spectric has half of the answer really
$("span.link").click()
will click on the span but you incorrectly set the click function
$("#span").on('click',function(e){
the # is for ids not for all elements. So it should be changed to
$("span").on('click',function(e){
or better yet to be more specific like #Spectric said
$("span.link").on('click',function(e){
I'm using JQuery in my project, and the only line it gets used is the following:
$('#div').on('input', function() { ... });
Is there a more lightweight lib/polyfill I can use instead, which allows to register an input event on a contenteditable div?
In pure javascript you can do something likes that :
document
.getElementsByTagName("div")
.addEventListener('input', function() { ... }));
And add this html attribute contenteditable="true" on your div.
I made a simple plugin to insert HTML into an inline CKEditor. But after every insert, the editor loses focus. Is there a way to keep focus and keep the cursor active after inserting the HTML?
editor
.addCommand("InsertHtml",
{
exec: function ()
{
editor.insertHtml("<span>xxx</span>");
}
});
If using insertElement instead of insertHtml the focus is kept.
var newElement = CKEDITOR.dom.element.createFromHtml("<span>xxx</span>", editor.document);
editor.insertElement(newElement);
You can try to dynamically put focus back on the editor with javascript using the .focus() function on the editor input.
editor.addCommand("InsertHtml",
{
exec: function () {
editor.insertHtml("<span>xxx</span>");
document.getElementById("yourCKEditorInputId").focus();
}
});
I want to implement simple pub/sub pattern with jQuery.
So I add some code like this on parent page:
Parent page:
$(document).bind('custom', function() { ... });
And it's working fine when I trigger on same page like this:
Same Page:
$(document).trigger('custom'); // Working.
But when I trigger this on popup page, it's not working.
Popup page:
opener.$(document).trigger('custom'); // Not working.
$(opener.document).trigger('custom'); // Not working.
If I bind event to <body> element, it works find.
Parent Page:
$('body').bind('custom', function() { ... });
Popup Page:
opener.$('body').trigger('custom'); // Working.
Why binding to document is not working on Popup?
As #11684 said, the answer to make it work is:
opener.$(opener.document).trigger('custom');
The #Rory's answer:
$(opener.document).trigger('custom');
is not working because the popup's $ doesn't have the event handler of opener.document.
And this one:
opener.$(document).trigger('custom');
is not working because the document is popup's document so it's different from the opener.document.
And lastly,
opener.$('body').trigger('custom');
is working because the opener's $ has event handler and the argument(body) is just string(not a object like document).
You need to place the whole opener.document in to a jQuery object. Try this in the popup:
$(opener.document).trigger('custom');
I have a tinyMCE textarea #frmbody and am using the jquery instance of it.
<textarea class="tinymce" name="frmbody" id="frmbody" cols="30" rows="20"></textarea>
I'm trying to get the contents of the textarea as the user types.
$("#frmbody").live('keyup', function(e) {
alert("keyup");
});
The code above is not working and I'm not sure why. If I remove the tinyMCE instance, the code above works fine. Any ideas?
That's because an instance of TinyMCE isn't a true textarea, so keyup events aren't detected. Try the onchange callback instead.
You can make tinyMCE own listener by:
http://www.tinymce.com/wiki.php/API3:event.tinymce.Editor.onKeyUp
or write your own and use built-in function getContent:
http://www.tinymce.com/wiki.php/API3:method.tinymce.Editor.getContent
One could just grab the contents of the editor using :
tinymce.activeEditor.getContent();
If you want to attach an onchange event, according to this page, http://www.tinymce.com/wiki.php/api4:class.tinymce.Editor, create the editor using the following code :
var ed = new tinymce.Editor('textarea_id', {
init_setting_item: 1,
...
}, tinymce.EditorManager);
ed.on('change', function(e) {
var content = ed.getContent();
$("#textarea_id").val(content); // update the original textarea with the content
console.log(content);
});
ed.render();
Please note that onchange is fire when the user unfocuses, press enter, or press a toolbar button instead of every keystroke.
It is very easy to write an own handler and assign it to the editor iframe document.
There are tinymce handlers for various events already defined like keyUp
Here is the standard way to assign an own handler to the editor iframe document
var my_handler = function(event) {
alert('my handler fired!');
};
var ed = tinymce.get('my_editor_id');
$(ed.getDoc()).bind('keyup', my_handler);
TinyMCE hides the Text Area and creates a html container. If you write content in the box its a iFrame with the name "TEXTAREANAME_ifr".
So try this:
$("#frmbody_ifr").live('keyup', function(e) {
alert("keyup");
});
I think as Thariama already said the EventHandler from TinyMCE would be the best way.