JQuery – How Do I Place a DIV On Overlay


I'm trying to make a modal dialog in the least amount of jQuery code as possible because my project already has a bit too much jQuery loaded into it.

So, I first needed an overlay, which is achieved with:

$('body').wrapInner('<div />')

Disregard for now that I have another routine to kill the click events on #dim1 while this modal is present. So, now I need to draw my modal dialog on top:

$('body').append('<div id="test">My Test</div>');

However, when I do this, I end up with a dimmed out #test, when I don't want that to be dimmed — just the stuff behind it. What's the trick?

Best Solution

See this quick demo here, the key lies to the modal being absolute over the top of the opaque background. The demo dynamically adds the background overlay div and the modal div to the body but you could define these in the html and just show them.

Agreed there is no need to use a plugin for a modal effect, most of the plugins come with lots of options hence the bloat which you do not want if you only need the simplest of effects. - Note we can do this in two lines rather than 3 - and without a plugin!!

Also it is far easier to define css classes and add these rather than add inline styles to an element in script. Easier to maintain.