1

hi I want to disable click after the user clicked on an element

the elements loaded after my script (elements built by javascript) and I can't use these code

     $('.disableClick').click(
            function (e) {
                e.stopPropagation();
                return false;
            }
        )

and I use these code to do work on the element by click

    $('body').on('click','.disableClick',
                function (e) {
                    if(!is_valid)
                    {
                        e.preventDefault();
                        e.stopPropagation();
                        return false;
                    }
                }
            );

I think disabling click codes like e.stopPropagation() don't support for

$('body').on('click') event

Mahdi mehrabi
  • 668
  • 8
  • 16
  • 1
    For dinamic elements you need to use like this `$(document).on('click', '.element', function(){ [...] })` – Roy Bogado May 30 '19 at 06:39
  • Possible duplicate of [How to bind click event to button from ajax result](https://stackoverflow.com/questions/49944236/how-to-bind-click-event-to-button-from-ajax-result) – Roy Bogado May 30 '19 at 06:41

2 Answers2

1

try this:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
 <script type="text/javascript">
    $('body').on('click','.disableClick',
            function (e) {

                   $(this).attr("disabled", true);
            }
        );
</script>

It works on body as well as on document both

1

You need to use unbind I guess. Try

    if(!is_valid){
    $('selector').unbind("click")
    }
Zabih Ullah
  • 465
  • 3
  • 13