0

I am creating elements using jQuery AJAX and need to make it so when a button is clicked, a page is loaded that preforms a click on a button within it.

This means that when the new data is loaded, a click event needs to be trigged that loads a modal into the form without the user physically clicking the button.

The page that is loaded in looks like this:

<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal" style="display: none;" id="myModalButton">Open Modal</button>

<div id="myModal" class="modal fade" role="dialog">
    <div class="modal-dialog">

        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Modal Header</h4>
            </div>
            <div class="modal-body">
                <p>Some text in the modal.</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>

    </div>
</div>

And the jQuery AJAX as such:

$( document ).ready( function ()
{
    $( "#limit_results" ).on( "click", function ()
    {
        $.ajax( {
            url: "timesheets.libs/items/modal.php",
            type: "GET",
            success: function ( r )
            {
                //alert( "" ); //Used to test AJAX was working
                $( "#bottom" ).html( r );
                $( "#bottom" ).on( "load", "#myModalButton", function ()
                {
                    //How to make it click????

                    // FAILED
                    //$( window ).load( function ()
                    //{
                    //    $( '#myModal' ).modal( 'show' );
                    //} );

                    // FAILED
                    //$( "#bottom" ).$( "#myModalButton" ).click();
                } );
            }
        } );
    } );
} );

The expected behaviour is as such:

  • User clicks first button (`limit_results`)
  • AJAX loads the modal into the view but does not show it
  • In the same AJAX call as prior, the button on the modal form is triggered by javascript/jQuery (not the user) and shows the modal to the user (`myModalButton`)

note
jQuery and BootStrap version I am using:

<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
Can O' Spam
  • 2,718
  • 4
  • 19
  • 45
  • @ShailendraSharma, this isn't form validation, this is **event handling** and the answers from the question marked as answering **do not answer my question** – Can O' Spam Oct 28 '15 at 10:12
  • https://www.google.co.in/search?client=ubuntu&channel=fs&q=button+click+on+a+dynamically+created+element&ie=utf-8&oe=utf-8&gfe_rd=cr&ei=zp8wVpayKND08weJvrbwDA#channel=fs&q=button+click+on+a+dynamically+created+element+site:stackoverflow.com – Shailendra Sharma Oct 28 '15 at 10:14
  • have you check above google link, if yes got somthing or not ? – Shailendra Sharma Oct 28 '15 at 10:15
  • @ShailendraSharma, afraid not as yet, looking throught now. Thank you for that – Can O' Spam Oct 28 '15 at 10:16
  • 1
    If not dupe, please describe better expected behaviour – A. Wolff Oct 28 '15 at 10:16
  • @A.Wolff, I have amended to show the expected behaviour, the behaviour differes from the question marked as dupe because the OP in the question wants the user to trigger the click, where I want it automatic with the button not to be seen. – Can O' Spam Oct 28 '15 at 10:19
  • Does it fix it: `$('#bottom').find('.btn-lg').click();` ??? – A. Wolff Oct 28 '15 at 10:21
  • It didn't, for some reason (even though the ajax loads the page, I made it show the button), it still does not preform the click... – Can O' Spam Oct 28 '15 at 10:23
  • @SamSwift Ok but if you click on button once ajax load done, is it working at least? Meaning is the modal shown as expected? – A. Wolff Oct 28 '15 at 10:25
  • Yes, the modal shows as expected, but for some reason the jQuery `.click()` doesn't seem to trigger the event to occur – Can O' Spam Oct 28 '15 at 10:26
  • And what about (still in load() complete callback): `$(this).find('#myModal').fadeIn();`? Is there btw any duplicate IDs? And maybe try: `$(this).find('.btn-lg')[0].click();`. Not sure which plugin you are using for modal – A. Wolff Oct 28 '15 at 10:27
  • There are no dupe ID's (checked it about 4 times for them! Haha), and for some reason it still isn't working with `fadeIn()`, could it be the version of jQuery I am using? – Can O' Spam Oct 28 '15 at 10:28
  • It could but this would throw error in console. Now try to provide [MCVE](http://stackoverflow.com/help/mcve) (maybe without the ajax part but just the modal plugin used and relevant HTML markup/CSS – A. Wolff Oct 28 '15 at 10:29
  • Very odd... Maybe a good ole Google is needed to see if this is an error known about... – Can O' Spam Oct 28 '15 at 10:29
  • But is it bootstrap modal or what??? – A. Wolff Oct 28 '15 at 10:31
  • @A.Wolff, it is, I am using BS 3.3.5 for this – Can O' Spam Oct 28 '15 at 10:32
  • Again, you should provide MCVE. Here as i can test it, it works as expected: http://jsfiddle.net/opj50p69/ So test what you get for: `console.log($(this).find('.btn-lg').length);` – A. Wolff Oct 28 '15 at 10:35
  • I was saying that `$( "#bottom" ).$( "#myModalButton" ).click();` should be `$( "#bottom #myModalButton" ).click();`. And again, it should not be in that code block. It should come right after: `$( "#bottom" ).html( r );`. Did you get it? – Ifedi Okonkwo Oct 28 '15 at 10:53
  • http://api.jquery.com/live/ should do the trick – Mihnea Belcin Oct 28 '15 at 11:06
  • @MihneaBelcin, afraid not, ` Deprecated 1.7`, I am using 1.11 – Can O' Spam Oct 28 '15 at 11:10

2 Answers2

0

I can't see "myModalButton" in your loaded html.

If its there (i.e. you need to give that ID to the button), then this should work:

$( "#bottom" ).on( "click", "#myModalButton", function ()
    {
        alert('modal button clicked');
    }

I'm assuming that "#button" is the ID of a container existing on the page before the AJAX request.

EDIT, based on your edit of the question:

If an event function had been attached to that button, then all you need to do after the AJAX returns is:

$('#myModalButton').click();

Please note that with your given code, you have not shown the code that would assure this. The code block that says $( "#bottom" ).on( "load",... does not need to be there.

 

EDIT 2: This thread is now rather cluttered with comments, and I'm not sure we're not getting lost in it.

If you say that clicking that #myModalButton button, when manually clicked, does what you want, then this should be your AJAX code:

$( document ).ready( function ()
{
    $( "#limit_results" ).on( "click", function ()
    {
        $.ajax( {
            url: "timesheets.libs/items/modal.php",
            type: "GET",
            success: function ( r )
            {
                //alert( "" ); //Used to test AJAX was working
                $( "#bottom" ).html( r );
                $( "#bottom" ).find('#myModalButton" ).click();
            }
        } );
    } );
} );
Ifedi Okonkwo
  • 3,406
  • 4
  • 33
  • 45
  • Wouldn't this be a user action? I need it to be automatic without user action on the button... – Can O' Spam Oct 28 '15 at 10:22
  • Yes it would. Ooops, you made edits. So you want an action to just happen after the AJAX load and without user interaction? – Ifedi Okonkwo Oct 28 '15 at 10:25
  • Yes, that's exactly it – Can O' Spam Oct 28 '15 at 10:27
  • Also, the `myModalButton` is declared at the top of the form with the modal on it, just have to scroll due to the size limitations on the boxes we are given! Haha – Can O' Spam Oct 28 '15 at 10:30
  • Ok. I've seen it ;). So can you show the code showing the action you want taken on clicking the button? I hope it is delegated. – Ifedi Okonkwo Oct 28 '15 at 10:33
  • The only piece of code missing is the main `index.php` page and the custom stylesheet needed for the BS modal... – Can O' Spam Oct 28 '15 at 10:34
  • Did you confirm that all this html is loaded correctly? (e.g. console.log) and the problem is just to make it visible to user? Sorry I don't have specific experience with BS Modal. – Ifedi Okonkwo Oct 28 '15 at 10:39
  • The HTML loads as expected, I can click the button myself and get it to show the modal perfectly fine by hand, but this is not what is needed, I need to show a message to the user themselves automatically every time the `#limit_results` button is clicked – Can O' Spam Oct 28 '15 at 10:41
  • ` //$( "#bottom" ).$( "#myModalButton" ).click();` (that's what you have) will fail; ` //$( "#bottom" #myModalButton" ).click();` shouldn't. – Ifedi Okonkwo Oct 28 '15 at 10:43
  • It doesn't unfortunately, it throws an error both in IDE and in the console as there is no identifier given for the `#myModalButton` – Can O' Spam Oct 28 '15 at 10:45
0

Put below statement on ready event.These statement call '#myModalButton' click event after ready of document.You don't need to perform click event for button having id as 'myModalButton'.

$( "#myModalButton" ).trigger( "click" );
Domain
  • 11,562
  • 3
  • 23
  • 44