I have a simple code that counts number of words in a sentence. This word counter has 3 parts, a CSS, JS & HTML files. I know its possible to run this scripts in WordPress but I am not sure how to achieve this. Below is the codes:
JS:
$(function() {
$("#sys-tbox").each(function() {
var input = '#' + this.id;
counter(input);
$(this).keyup(function() {
counter(input);
});
});
});
function counter(field) {
var number = 0;
var text = $(field).val();
var word = $(field).val().split(/[ \n\r]/);
words = word.filter(function(word) {
return word.length > 0
}).length;
$('.words').text(words);
$('.character').text(text.length);
}
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Sysadmin Word Counter</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="js/jscript.js"></script>
<link rel="stylesheet" href="css/css-style.css" />
</head>
<body>
<div id="sys-mbox">
<textarea id="sys-tbox"></textarea>
</div>
<div class="sys-results">
Total number of words: <span class="words"> </span>
Total number of chars: <span class="character"> </span>
</div>
</body>
</html>
Here is a Fiddle
I have read in WordPress document that this can be achieved using wp_enqueue_script() but I am not sure where to start from since its appears the methods might differe based on the WordPress theme used since file structure's are different.
Please can anyone point me in the right direction or provide me with links to similar answered questions.
Thanks
Added this code to Function.php:
function wordcount_scripts()
{
// Register the script like this for a plugin:
wp_register_script( 'wordcountjs', plugins_url( '/js/wordcountjs.js', __FILE__ ) );
// or
// Register the script like this for a theme:
wp_register_script( 'wordcountjs', get_template_directory_uri() . '/js/wordcountjs.js' );
// For either a plugin or a theme, you can then enqueue the script:
wp_enqueue_script( 'wordcountjs' );
}
add_action( 'wp_enqueue_scripts', 'wordcount_scripts', 5 );