One possible solution is that which was recommended by Ankit (he seems to have deleted his post). Here is a jsfiddle example of it in action: http://jsfiddle.net/drb9w/11/. This does have the weakness, however, of the browser styles for focused objects being applied to the #share
element.
$('#moresh').click(function (event) {
var elt = $(this).attr('for');
$("#" + elt).toggleClass("open");
$("#share").focus();
});
$(function () {
$("#share").draggable();
});
$("#share").on('blur', function(event) {
$('#shead').removeClass("open");
});
How it works:
- Focus the
#share
element so that it can be blurred (unfocused).
- When the
#share
element is blurred, remove the open
class.
If you want to maintain the functionality of your 'for' attribute, use the data argument of the on function.
There is an alternative method which avoids the problem of the focus decorators being applied to #share
: register a click listener to the parent element (which seems to cover the entire screen) which is prevented from activating by the #share
element like so:
$('#moresh').on('click', function(event) {
var elt = $(this).attr('for');
$("#" + elt).toggleClass("open");
function onBodyClick(event) {
$(this).off('click', '', onBodyClick);
$(this).removeClass('open');
}
$("#" + elt).on('click', onBodyClick);
});
$(function() {
$("#share").draggable();
});
$("#share").on('click', function(event) {
event.stopPropagation();
});
http://jsfiddle.net/trG2n/6/