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.
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.
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.
I am trying to write some code for change() event using jQuery Text Editor (jqte), I have two functions which give jqte functionality to textarea's
One for editors loaded with JavaScript, when clicking some elements in a page:
function onLoadEditor(){
jQuery(".comment-editor").jqte({
// some jqte params, such as fsize: false,indent: false...
change: function(){ observeEditor(); }
});
}
And other, generic function, for pages with one single editor
jQuery(function() {
jQuery(".comment-editor").jqte({
// some jqte params, such as fsize: false,indent: false...
change: function(){ observeEditor(); }
});
});
I want to access the id of the concrete textarea (all textareas in the page have an id) which has fired the change() event
How should I write observeEditor() function to achieve this? Or... how I should define the function in jqte change property?
After reading this jQuery blur event with ID and value I have solved it, with following code (simplified)
function onLoadEditor(){
jQuery(".comment-editor").each(function(idx, elem) {
jQuery(this).jqte({
// some jqte params, such as fsize: false,indent: false...
change: observeEditor(elem.id),
});
}
jQuery(function() {
onLoadEditor();
});
But now I have another problem...
As you can read in the original question, onLoadEditor() is called when clicking some elements in a page. Then another javascript function jsComment() is called, builds a form (with a textarea.comment-editor field included) and it is rendered this way
function jsComment(){
...
var form = '<div class="comments_wrapper ... ';
jQuery(form).insertAfter(some_element).fadeIn('fast');
onLoadEditor();
}
Problem is change() event is being fired only once, when form fades in, while the idea is the opposite, event should fire when user adds some text, not when appearing... Any tips?
UPDATE
After reading Event binding on dynamically created elements? I have solved it this way
function onLoadEditor(){
jQuery('.comment-editor').each(function(idx, elem) {
jQuery(this).jqte({
// some jqte params, such as fsize: false,indent: false...
});
jQuery(document).on('change',
jQuery('.comment-editor'),
function(){
observeEditor(elem.id);
}
);
});
}
jQuery(function() {
onLoadEditor();
});
Although finally I am not using change() event, as it was being fired constantly. Performing better with keyup() & paste(), for instance
I have a page that has a lot of JS created elements. So i wish to focus a textarea after it appear. I try to make it with help of addEventListener like so:
mytextarea.addEventListener('someEvent', function(e)
{
this.focus();
});
My problem is, that i can not found the right Event, that would be fired at the moment, when textarea get appended in the document, like here
document.getElementsByTagName('body')[0].appendChild(mytextarea);
Native JS Only please. Thank you
Have you tried this:
document.getElementsByTagName('body')[0].appendChild(mytextarea);
mytextarea.focus();
FIDDLE
this should work:
<textarea autofocus></textarea>
Put your focus in
$(document).ready(function(){
//Here
});
This mean your textarea is appended to the document.
And without jQuery :
window.addEventListener('load', function () {
//here
});