Reputation: 153
I'm making a simple chat using jQuery. The problem is with form? which contains of an input with type "text" and a button. In my JS code I make the button disabled if the text field is empty and enable it when some text is tyoed in it. It worked properly as I had followig code:
home.html:
{% load staticfiles %}
<html>
<head>
<title>Public chat</title>
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<!-- JavaScript -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
<!-- Here are the previous messeeges showed, it doesn't matter now-->
<form id="chat-form" method="post" action="/post/">
<div id="chat-bottom" class="input-group">
<input type="text" id="chat-msg" name="chat-msg" class="form-control"/>
<span class="input-group-btn">
<input class="btn btn-default" id="send" type="submit" value="Send"/>
</span>
</div>
</form>
</body>
<script src="{% static 'chat.js' %}"></script>
</html>
chat.js:
$('#chat-form').on('submit', function(event){
event.preventDefault();
$.ajax({
url : '/post/',
type : 'POST',
data : { msgbox : $('#chat-msg').val() },
success : function(json){
$('#chat-msg').val('');
$('#msg-list').append('<li class="text-right list-group-item">' + json.msg + '</li>');
var chatlist = document.getElementById('msg-list-div');
chatlist.scrollTop = chatlist.scrollHeight;
}
});
});
function getMessages(){
if (!scrolling) {
$.get('/messages/', function(messages){
$('#msg-list').html(messages);
var chatlist = document.getElementById('msg-list-div');
chatlist.scrollTop = chatlist.scrollHeight;
});
}
scrolling = false;
}
var scrolling = false;
$(function(){
$('#msg-list-div').on('scroll', function(){
scrolling = true;
});
refreshTimer = setInterval(getMessages, 500);
});
$(document).ready(function() {//here checking if the text field is empty
$('#send').attr('disabled','disabled');
$('#chat-msg').keyup(function() {
if($(this).val() != '') {
$('#send').removeAttr('disabled');
}
else {
$('#send').attr('disabled','disabled');
}
});
});
When I tried to add TinyMCE to the text field
<script type="text/javascript" src="{% static 'tiny_mce/tiny_mce.js' %}"></script>
<script type="text/javascript">
tinyMCE.init({
theme : "advanced",
selector : "#chat-msg"
});
</script>
the "Send" button is always disabled. I tried to do like here Get value of tinymce textarea with jquery selector so function looked like this:
$('#chat-form').on('submit', function(event){
event.preventDefault();
tinyMCE.triggerSave();
$.ajax({
//here the same code as above in the same function
});
});
and this:
$(document).ready(function() {
$('#send').attr('disabled','disabled');
$('#chat-msg').keyup(function() {
tinyMCE.triggerSave();
if($(this).val() != '') {
$('#send').removeAttr('disabled');
}
else {
$('#send').attr('disabled','disabled');
}
});
});
vut the reault is still the same. At first I didn't want to use getContent() (as it is said in many similiar questions) because in the documentation (http://archive.tinymce.com/wiki.php/API3:method.tinymce.Editor.getContent) it is said to make cleanup of content. Finally I tried even that with no effect.
I am new both to jQuery and TinyMCE, so I beleive that I'm missing something obvious. How should chat.js be changed here to retreive the content of text field?
Upvotes: 1
Views: 2152
Reputation: 556
In my case tinyMCE.triggerSave(); was not working so I set the content manually at onChange Event . you can do it like this.
<script type="text/javascript">
tinyMCE.init({
theme : "advanced",
selector : "#chat-msg"
setup: function (editor) {
editor.on('change', function () {
var content = tinyMCE.activeEditor.getContent().trim();
var tinyMceElement = tinyMCE.get(editor.editorId).getElement();
$(tinyMceElement).html(content);
});
});
</script>
Note : var content = tinyMCE.activeEditor.getContent().trim(); var tinyMceElement = tinyMCE.get(editor.editorId).getElement();
This is the way to get text content and the dom element in old tinyMce it may be diffrent in new Version.
Upvotes: 1
Reputation: 13726
The first thing I would change is your keyup
function. When TinyMCE is injected into the page the original form element (<input>
in your case) is no longer visible. If you use your browser tools you will see that TinyMCE injects a series of <div>
s and an <iframe>
effectively hiding your <input>
. Once you do this the code you have in document.ready
won't ever trigger:
//This won't trigger anything with TinyMCE on the page
$('#chat-msg').keyup(function() {
tinyMCE.triggerSave();
if($(this).val() != '') {
$('#send').removeAttr('disabled');
}
else {
$('#send').attr('disabled','disabled');
}
});
Instead you can add a keyup
or change
function to TinyMCE's init function:
tinymce.init({
selector: 'textarea',
...
setup: function (editor) {
editor.on('change', function () {
//Perform your updating of the button here
});
editor.on('keyup', function () {
//Perform your updating of the button here
});
}
});
Upvotes: 1