I have a problem getting some script to run in a wordpress widget when it is needed. Ideally I would like to be able to hook into the javascript event fired when a widget is dropped into a sidebar, or when the widget is opened up for the first time to run some setup javascript.
Unfortunately script tags contained in the widget form itself are stripped out unless the widget is already in a sidebar, so the problem exists when a new widget is added dynamically and hasn’t been saved yet.
I’ve got a nasty hacky solution using a jquery .live() event binding on mouseover of the widget to fire off my code but I’d like to think there’s a better way.
Just to clarify, as of WordPress 3.2.1, the exact selector you would use would be “div.widgets-sortables”, so the code would look like:
I got the tumbleweed badge for this one!
Anyway, the answer is: