EDIT
Since I misread my own code, that would instantiate a blocker for every element being blocked, I present another solution, which accepts a selector as a option and thus allows the widget to be created once, and bring forward the elements that are matched by the selector. Based on the above Fiddle, there are only changes to the JS part: http://jsfiddle.net/DzEFx/1/
(function ($) {
$.widget('my.blocker', {
options: {
selector: null
},
_create: function() {
var selector = this.options.selector || this.element;
this.$blocker = $('<div/>', {
css: {
backgroundColor: '#000000',
opacity: 0.4,
position: 'fixed',
top: 0,
bottom: 0,
left: 0,
right: 0,
zIndex: 99,
}
});
selector.each(function(index) {
var element = $(this); // the current element is passed as the this value
var oldZIndex = element.css("z-index");
element.data("old-z-index", oldZIndex);
element.css("z-index", 1000);
});
this.$blocker.appendTo($('body'));
return this._super();
}
});
$('#btnBlock').click(function() {
var anElement = $('.unblocked');
$(document.body).blocker({selector: anElement});
});
})(jQuery);
You can see that the widget now has a options object, whose properties can be overrided as a object passed to the first argument when creating the widget.
OLD ANSWER
You need to use a class for the widgets you want to allow to be visible, and use the according selector. See this Fiddle:
http://jsfiddle.net/EQsA7/ Notice how the desired elements have
class="unblocked" on them, and look at the modified button click handler.
HTML:
<div><input type="text" value="blocked"></div>
<div class="unblocked" id="unblocked1"><input type="text" value="unblocked"></div>
<div><input type="text" value="blocked"></div>
<div class="unblocked" id="unblocked2"><input type="text" value="unblocked"></div>
<div><input type="text" value="blocked"></div>
<input type="button" id="btnBlock" value="Block">
JS:
(function ($) {
$.widget('my.blocker', {
_create: function() {
this.$blocker = $('<div/>', {
css: {
backgroundColor: '#000000',
opacity: 0.4,
position: 'fixed',
top: 0,
bottom: 0,
left: 0,
right: 0,
zIndex: 99,
}
});
// this.element will refer to unblocked1
// I want to access unblocked2 aswell
this.originalZIndex = this.element.css('z-index');
this.element.css('z-index', 100);
this.$blocker.appendTo($('body'));
return this._super();
}
});
$('#btnBlock').click(function() {
var anElement = $('.unblocked');
anElement.blocker();
});
})(jQuery);