Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clicking button only once (js)

I looked over stackoverflow and other websites, but could not find the answer I am looking for.

I have a button of sorts, and I want users to ONLY be able to click on it once. This is my javascript/jquery triggering the click event. But how can I force it so that it can ONLY be clicked once?

 $(document).ready(function () {
     //var $new_total = $('#gtotal').val();

     $('#a_is_valid').click(function () {
         if ($('#code_promo').val() == 'theCode') {
             $('#gtotal').val($('#gtotal').val() - ($('#gtotal').val() - (($('#gtotal').val() * .75))));
         }
     })
 })
like image 943
M.Sidim Avatar asked Apr 22 '13 20:04

M.Sidim


1 Answers

Use jQuery's .one() function.

$('#a_is_valid').one('click', function(){
    if ($('#code_promo').val() == 'theCode')
    {$('#gtotal').val($('#gtotal').val()-($('#gtotal').val()-(($('#gtotal').val()*.75))));
}
like image 145
j08691 Avatar answered Sep 29 '22 19:09

j08691