Reputation: 1278
I want to change the position of an HTML element inside a UIWebView when I click a button. I have written the following code for that but it doesnt seem to work. Anyone know what I could be doing wrong?
NSString *str = [NSString stringWithFormat:@"function setOffset() {\
var offset = document.getElementById('cimage').offset();\
document.getElementById('cimage').offset({ top: offset.top-50, left: offset.left});\
setOffset();"];
[self.webView stringByEvaluatingJavaScriptFromString:str];
Upvotes: 0
Views: 156
Reputation: 6403
Your UIWebView
is a Javascript interpreter that needs to be fed correct Javascript.
1) Your braces are not balanced so the code won't evaluate properly 2) You are only trying to define a function, not calling it (for a quick-fix you could try removing the first line function setOffset() {
, instead letting it start at var offset =
..., that would mean instant execution instead of function definition).
I suggest you try out your Javascript/DOM interactions in an actual live Javascript environment (like Chrome's Developer Tools) and ensure that the code is correct and does what you expect it to do in your lab environment first. UIWebView
's are not that much different from desktop web browsers.
You can also get a rather good development environment for your actual instance of UIWebView
by following the directions given in this answer.
Upvotes: 1