Question

I am working with one jQuery library that allows to open modal windows by clicking a link. The point is that I want to open the window but without clicking any link. I know it's a little strange, but it is possible to call the modal without having the trigger link (where the reference to the modal library is)?

Update: What I have tested

<script type="text/javascript">
    $(function() {
        $('a[rel*=leanModal]').leanModal({ top : 100, closeButton: ".modal_close"});        
    });
</script>

...
<!-- a rel="leanModal" href="#modal" name="modal">Link</a -->
...

<script type="text/javascript">
    $('#modal').click();
</script>
<div id="modal">
</div>

The reference to the modal div is in link, but I don't have that link and it won't be created.

Reference: leanModal.js

Was it helpful?

Solution

Just trigger the link click where you want.

$(".myLink").trigger('click'); // or:
$(".myLink").click();

Edit: In your question you're using the wrong handler. $("modal") would look for a <modal> element. You're looking for $('#modal').

If you want to have the dialog pop up without a link at all, you should be able to set it hidden using display: none in the CSS or style attribute, and then trigger it via code anyways.

Or, you can use Fancybox for added flexibility, and they're more open about how to use their dialog boxes without attaching to a specific link, but rather any element and decide what goes inside and how to trigger it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top