Start loading div by clicking on a specific button

I use this javascript

one to show the hidden div for the loading message when I calculate in the background:

 <script src="Scripts/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        function ShowProgress() {
            setTimeout(function () {
                var modal = $('<div />');
                modal.addClass("modal");
                $('body').append(modal);
                var loading = $(".loading");
                loading.show();
                var top = Math.max($(window).height() / 2 - loading[0].offsetHeight / 2, 0);
                var left = Math.max($(window).width() / 2 - loading[0].offsetWidth / 2, 0);
                loading.css({ top: top, left: left });
            }, 200);
        }
        $('form').live("submit", function () { 
            ShowProgress();  
        });  
    </script>

      

As I understand it, the script always works if supplied with submit

$('form').live("submit", function () { 
            ShowProgress();  
        });  

      

How can I change the code to launch only for a specific button? I tried loading it on button click (commented $('form').live("submit",...

)

protected void btn_start_Click(object sender, EventArgs e)
    {
        string script = "<script type=\"text/javascript\"> ShowProgress(); </script>";
        ClientScript.RegisterClientScriptBlock(this.GetType(), "myscript", script);
    }

      

The loading div shows up but never stops. What should I change? Thanks to

Update Forgot code in if (!IsPostBack)

:

 string script = "$(document).ready(function () { $('[id*=btnSubmit]').click(); });";
 ClientScript.RegisterStartupScript(this.GetType(), "load", script, true);

      

+3


source to share


2 answers


$("#buttonId").click(function(){
  ShowProgress();
});

      



This method will run anytime a specific button is clicked (you obviously need to replace buttonId

in my code with the actual ID of the button you selected).

+2


source


As described in the documentation here , the method live

will apply to all submit events. As suggested by other answers, you only need to select one item and use the event onclick

or check if the event target is your specific button.



I would go with the first one, but it depends on the behavior you want.

+1


source







All Articles