Mahdi mehrabi
Mahdi mehrabi

Reputation: 1744

disable click after clicking on $('body').on('click') event

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

Upvotes: 1

Views: 1790

Answers (2)

Zabih Ullah
Zabih Ullah

Reputation: 605

You need to use unbind I guess. Try

    if(!is_valid){
    $('selector').unbind("click")
    }

Upvotes: 1

Pallavi Sharma
Pallavi Sharma

Reputation: 140

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

Upvotes: 1

Related Questions