Reputation: 23025
Here is my code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>
function count()
{
var listOfWords, paragraph, listOfWordsArray, paragraphArray;
var wordCounter=0;
listOfWords = document.getElementById("wordsList").value;
//Split the words
listOfWordsArray = listOfWords.split("\n");
//Get the paragrah text
paragraph = document.getElementById("paragraph").value;
paragraphArray = paragraph.split(" ");
//check whether paragraph contains words in list
for(var i=0; i<listOfWordsArray.length; i++)
{
if(paragraph.contains(wordListArray[i]))
{
wordCounter++;
}
}
window.alert("Number of Contains: "+wordCounter);
}
</script>
</head>
<body>
<center>
<p> Enter your Word List here </p>
<br />
<textarea id="wordsList" cols="100" rows="10"></textarea>
<br />
<p>Enter your paragraph here</p>
<textarea id="paragraph" cols="100" rows="15"></textarea>
<br />
<br />
<button id="btn1" onclick="count()">Calculate Percentage</button>
</center>
</body>
</html>
Here, what I am trying to do is counting how any number of words are in paragraph
which are also included in wordList
. words in wordList
are separated by new line.
However, I am not getting anything as the output here. I am not into web and scripting languages much so I failed to find what is going behind.
How can I count how many words are in paragraph
which are also included in wordList
? And please explain why it is not getting displayed?
Upvotes: 0
Views: 694
Reputation: 18008
Use indexOf
method of string if you just want occurrence. Use RegExp
to check the whole word only. Change your conditional like this (counts whole word):
//check whether paragraph contains words in list
for (var i = 0; i < listOfWordsArray.length; i++) {
re = new RegExp("\\b" + listOfWordsArray[i] + "\\b");
if (paragraph.match(re)) {
wordCounter++;
}
}
Check here: http://jsfiddle.net/EuhEE/
Upvotes: 1
Reputation: 3740
If you know that the textarea is only going to have text in it,use this
var count = document.getElementById('content').innerHTML.split(' ').length;
If the textarea can have HTML tags in it, you're going to have to traverse its children looking for text nodes:
function get_text(el) {
ret = "";
var length = el.childNodes.length;
for(var i = 0; i < length; i++) {
var node = el.childNodes[i];
if(node.nodeType != 8) {
ret += node.nodeType != 1 ? node.nodeValue : get_text(node);
}
}
return ret;
}
var words = get_text(document.getElementById('content'));
var count = words.split(' ').length;
This is the same logic that the jQuery library uses to achieve the effect of its text() function. jQuery is a pretty awesome library that in this case is not necessary. However, if you find yourself doing a lot of DOM manipulation or AJAX then you might want to check it out.
Upvotes: 1
Reputation: 4234
You just had a typo
//check whether paragraph contains words in list
for(var i=0; i<listOfWordsArray.length; i++)
{
if(paragraph.contains(wordListArray[i]))
{
see wordListArray
doesn't exist, it should be listOfWordsArray
see here http://jsfiddle.net/PW7jZ/
Upvotes: 1