I am trying to run a javascript from a UIWebView. The javascript function I want to run is the one below
function SelectAnimal() {
var sel = document.getElementById('Animals');
var val = document.getElementById('AnimalToFind').value;
for(var i = 0, j = sel.options.length; i < j; ++i) {
if(sel.options[i].innerHTML === val) {
sel.selectedIndex = i;
break;
}
}
}
I know you can execute javascript in UIWebView using
(NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script
So i form the javascript string as
NSString *str = @"function SelectAnimal() {\
var sel = document.getElementById('Animals');\
var val = document.getElementById('AnimalToFind').value;\
for(var i = 0, j = sel.options.length; i < j; ++i) {\
if(sel.options[i].innerHTML === val) {\
sel.selectedIndex = i;\
break;\
}\
}\
}";
and run the javascript as
[webView stringByEvaluatingJavaScriptFromString:str];
But this doesnt seem to work. Is there something i'm doing wrong?