mp5163093
11-28-2016, 09:10 PM
Hello Everyone,
I am tyring to change the textarea to a div
<div id="editor" contenteditable="true" name="content"></div>
and is not working.
Anyone, do you have any idea why is not working when i use div tag ?
Any help is greatly appreciated
<div class="word-count">
<textarea id="editor" name="content" rows="30" cols="100"></textarea><br />
Character Count: <span class="chars"></span> |
Word Count: <span class="words"></span> |
Paragraph Count: <span class="paras"></span><br />
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
var wordCounter = {
el: '.word-count',
initialize: function() {
var _this = this;
$('div', this.el).on('keyup', function() { _this.update( $(this).text() ); });
$('div', this.el).on('keyup', function() { _this.update( $(this).text() ); });
},
update: function(value) {
$('.chars', this.el).html( this.getCharCount(value) );
$('.words', this.el).html( this.getWordCount(value.trim()) );
$('.paras', this.el).html( this.getParaCount(value.trim()) );
},
/* getCharCount:
* - Calculates the number of characters in the field. - Counts *all* characters.
*/
getCharCount: function(value) {
return value.length;
},
/* getWordCount:
* - Calculates the number of words in the field. - Words are separated by any number of spaces or a semi-colon.
*/
getWordCount: function(value) {
if (value) {
var regex = /\s+|\s*;+\s*/g;
return value.split(regex).length;
}
return 0;
},
/* getParaCount:
* - Calculates the number of paragraphs in the field. - Paragraphs are separated by any number of newline characters.
*/
getParaCount: function(value) {
if (value) {
var regex = /\n+/g;
return value.split(regex).length;
}
return 0;
}
};
wordCounter.initialize();
</script>
I am tyring to change the textarea to a div
<div id="editor" contenteditable="true" name="content"></div>
and is not working.
Anyone, do you have any idea why is not working when i use div tag ?
Any help is greatly appreciated
<div class="word-count">
<textarea id="editor" name="content" rows="30" cols="100"></textarea><br />
Character Count: <span class="chars"></span> |
Word Count: <span class="words"></span> |
Paragraph Count: <span class="paras"></span><br />
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
var wordCounter = {
el: '.word-count',
initialize: function() {
var _this = this;
$('div', this.el).on('keyup', function() { _this.update( $(this).text() ); });
$('div', this.el).on('keyup', function() { _this.update( $(this).text() ); });
},
update: function(value) {
$('.chars', this.el).html( this.getCharCount(value) );
$('.words', this.el).html( this.getWordCount(value.trim()) );
$('.paras', this.el).html( this.getParaCount(value.trim()) );
},
/* getCharCount:
* - Calculates the number of characters in the field. - Counts *all* characters.
*/
getCharCount: function(value) {
return value.length;
},
/* getWordCount:
* - Calculates the number of words in the field. - Words are separated by any number of spaces or a semi-colon.
*/
getWordCount: function(value) {
if (value) {
var regex = /\s+|\s*;+\s*/g;
return value.split(regex).length;
}
return 0;
},
/* getParaCount:
* - Calculates the number of paragraphs in the field. - Paragraphs are separated by any number of newline characters.
*/
getParaCount: function(value) {
if (value) {
var regex = /\n+/g;
return value.split(regex).length;
}
return 0;
}
};
wordCounter.initialize();
</script>