Harry
Harry

Reputation: 3391

Toggle DIV in a UIWebView using a UIButton?

I am developing an app that contains a UIWebView. I want the user to be able to click on a UIButton (or UIButtonBarItem) that will trigger a div element within the UIWebView to toggle on and off (using jQuery - $('shade').toggle();). Is this possible?

Names of elements..
UIWebView = forumIndex
UIButtonBarItem = forumMenuTrigger
The id of the div inside of the UIWebView = #shade

enter image description here

Upvotes: 0

Views: 173

Answers (2)

nhahtdh
nhahtdh

Reputation: 56809

In the handler for UIButton, you need to somehow grab the reference of the UIWebView. I assume you can do that on your own.

Let forumIndex be the name of the UIWebView. Use - (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script method to inject JavaScript into the UIWebView.

[forumIndex stringByEvaluatingJavaScriptFromString: @"$('#shade').toggle();"];

Upvotes: 1

stephenmuss
stephenmuss

Reputation: 2445

Something like this should work.

Providing you have an IBAction set for when the UIButton is tapped you can do something in the IBAction method like this:

[self.webView stringByEvaluatingJavaScriptFromString:@"$('#shade').toggle();"];

I'm assuming here that your view controller has defined the webview as a property (in this case "webView") - replace it with whatever is appropriate.

Upvotes: 1

Related Questions