Disable Button After Post Using Js/jquery June 19, 2023 Post a Comment I would like to have a function to add an onclick event to my form buttons to get them disabled in order to avoid double posting. Solution 1: The button must be an input element (if you don't submit the form via JavaScript). A <button> element has no attribute type and setting it will have no effect. I also suggest to attach the click handler via jQuery:<form><!-- ... --><inputid="submit"type="submit"value="post" /></form><scripttype="text/javascript> $('#submit').click(function() { $(this).attr('disabled', true); }); </script> CopyReference:click, attrSolution 2: $(button).attr('disabled', true); CopySolution 3: <button onclick="this.disabled='disabled'"type...> CopySolution 4: If you want to just target a button:$('#buttonID').click(function(){ $(this).attr('disabled', 'disabled'); }); CopyIf you want a function:functiondiableButton(bID){ $(bID).attr('disabled', 'disabled'); } CopyCall it using something like:$('#myform').submit(function(){ disableButton($('input[type=submit]', this)); }); Copy Share Post a Comment for "Disable Button After Post Using Js/jquery"
Post a Comment for "Disable Button After Post Using Js/jquery"