Hello,
I've tried to create a very simple input form to search for a specific word in a string. That said, I'm not having much luck. Firebug is alerting me of an exception error, and I can't figure out why. The code is below. The error msg says it's not a function.
function calculate() {
'use strict';
var output;
var sentence = document.getElementById('sentence');
var keyword = document.getElementById('keyword');
if(sentence && (sentence.value.length > 0)){
if(sentence.search(keyword) != -1){ //error msg here
output = keyword + " is in the sentence";
}else{
output = "sorry, no word exists";
}
document.getElementById('output').value = output;
}
return false;
}
function init(){
'use strict';
document.getElementById('stringTest').onsubmit = calculate;
}
window.onload = init;
I'm basing this on a smaller example I got to work, but it didn't involve form input. The following is that example.
var str = "this is a big dog";
var regExp = "cat";
if(str.search(regExp) != -1){
document.write(regExp + " was found");
}else{
document.write(regExp + " was not found");
}
Comments appreciated.
K