1

My modal window(jquery) has one link and two buttons. On click of the link I am replacing the current view with a new view using ajax call. The new view has two button Continue(btnContinue) and Cancel. The problem I am facing is the button click on the newly loaded view would not work.

My code :

       $(".ddlCart li").click(function (e) {

        var ddlselectedVal = $(this).attr('id');
        var agentId = $("#AgentId").val();

        var EnvironmentURL = $("#EnvironmentURL").val();

        var Action = "PreAddToCart"

        var postData = {
            AgentId: agentId,
            ActionTypeValue: Action
        };

        var close = function (event, ui) {
            $(this).dialog("destroy");
        }
        var open = function (event, ui) {
            var agentId = $("#AgentId").val();
            var url = EnvironmentURL + "/Stats/SearchContacts";

            $("#btncart_cancel").on("click", function () {
               $('#dvModalDialog').dialog("close");
            });


     $("#lnkCreateNewcart").on("click", function () {

                            var url = EnvironmentURL + "/MLSReports/Stats/Cart";
                            //Send the data using post and put the results in a div                   
                            $.post(url, {
                               ActionTypeValue: "CreateNewContact"
                            },
                                function (data) {
                                  // Replace current data with data from the ajax call to the div.         
                                    $("#dvModalDialog").empty().append(data);

                                });
                        }); 


//******* This is not firing ....
     $("#btnContinue").on("click", function () {
                            alert('This click is from the second View');
                        });      


        };

        var rd = Mod.ReportsDialog({
            title: 'Add To Cart',
            close: close,
            open: open
        });
        rd.url = EnvironmentURL + "/Stats/Cart";
        rd.targetElement = '#dvModalDialog'
        rd.formName = '#frmCart'
        rd.postData = postData
        rd.open();
        var $that = this;
        });
4
  • Try binding your 2 buttons outside the first click handler, perhaps? Commented Jun 17, 2014 at 22:10
  • Did you tried to rebind events for your fresh component ? When you're generating the new view in ajax, you have to bind ( with $('#idNewComponent').on(...) ) your new components. Commented Jun 17, 2014 at 22:11
  • I tried binding outside the first click handler Still didn't work. Commented Jun 17, 2014 at 22:15
  • Check the id of the button is it the same? check your console also. Try moving that function inside the second view instead of first Commented Jun 18, 2014 at 8:51

2 Answers 2

1

EDIT: 1

Let me know if this works:

$(document).on('click', '#btnContinue', function(e) {

instead of:

$("#btnContinue").click(function (e) {
Sign up to request clarification or add additional context in comments.

5 Comments

what would the code be if the button is type="submit"
See if this works: $(document).on('click', '#btnContinue input[type=submit]', function(e) {
What are you submitting? Try this: $(document).on('submit', '#btnContinue input[type=submit]', function(e) {
I need to validate the form.
`I need to validate the form. Tried this but not working. $(document).on('click', '#btnContinue , function (e) { $("#formCreateContact").validate({ rules: { Contact_FirstName: { required: true } } });
0

Instead of :

     $(".ddlCart li").click(function (e) {
...

use this:

$(".ddlCart li").on('click', function(e){

..

Because the first function only binds to the existing html elements on the page and doesn't recognize the new ones.

1 Comment

do you have the class that you are calling in the new element ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.