3

I need to remove button click event after press. How can I edit the function so whenever it's clicked, it gets disabled then runs the url, or vise-versa.

<form id="edit" action="" method="post">
    <input type="submit" name="button" onclick="this.value='test...'; MyFunction();" id="1" class="button blue" value="Submit">
</form>
<script>
    function MyFunction() {
        var url = "editing.php";
        document.getElementById("edit").setAttribute('action', url);
        return false;
    }
    $(".blue").click(function(){
        $(".blue").removeClass("disabled");
        $(this).addClass("disabled");
    });
</script>
Panos Kalatzantonakis
  • 12,525
  • 8
  • 64
  • 85
Howf Chat
  • 31
  • 1
  • 1
  • 3

7 Answers7

4

Remove the onclick, and do it the proper way :

<form id="edit" action="" method="post">
  <input type="submit" name="button" id="a1" class="button blue" value="Submit">
</form>
<script type="text/javascript">
$(function() {
    $('#a1').on('click', function() {
        $(this).val('test...').prop('disabled', true);
        $("#edit").attr('action', 'editing.php');
        return false;
    });
});
</script>
adeneo
  • 312,895
  • 29
  • 395
  • 388
2
$(".blue").click(function(){
  $(this).attr('disabled', true);
});
Community
  • 1
  • 1
martriay
  • 5,632
  • 3
  • 29
  • 39
  • `prop()` should be used here instead of `attr()`. See: [prop vs attr](http://stackoverflow.com/q/5874652/254830). For changing state, prop is appropriate. – doppelgreener Feb 06 '15 at 00:45
1

Just set the disabled attribute

$('.blue').attr('disabled', 'disabled');

JSFiddle


As @doppelgreener pointed out, prop is equally valid here

$('.blue').prop('disabled', true);

JSFiddle

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • `prop()` should be used here instead of `attr()`. See: [prop vs attr](http://stackoverflow.com/q/5874652/254830). For changing state, prop is appropriate. – doppelgreener Feb 06 '15 at 00:46
1

You can do this easily through jQuery using the attr() function:

$('.blue').click(function() {  
    $(this).attr('disabled', 'disabled');
});

By the way, I noticed that you're using jQuery. Why not 'jQuery-ify' your whole code:

function MyFunction() {
    var url = "editing.php";
    $("#edit").attr('action', url);
    return false;
}

$(".blue").click(function(){
    $(this).addClass("disabled").attr('disabled', true);
});
BenM
  • 52,573
  • 26
  • 113
  • 168
1

To make the .blue button clickable only once. The .one() method of jQuery is just the thing for you. Here is the documentation.

$("#edit > .blue").one("click", function() {
    //Code here will only run once.
}); 

"How can I edit the function so whenever it's clicked, it gets disabled then runs the url, or vise-versa."

I think you want the form to be submitted only once here; so:

<form id="edit" action="" method="post">
    <input type="submit" name="button" class="button blue" value="Submit" />
</form>

<script>
    //To make the form SUBMITTABLE ONLY ONCE:
    $("#edit").one("submit", function() { 
        $(".blue").prop("disabled", true); //you can still set the button disabled if you like
        $('#edit').attr('action', "editing.php"); //setting the action attribute here
    });
</script>

I don't know what your DOCTYPE is but if it's HTML4; you cannot have numeric ID for an HTML element. HTML5 seems to allow that. See this post.

Demo Fiddle here.

Community
  • 1
  • 1
Onur Yıldırım
  • 32,327
  • 12
  • 84
  • 98
0
<input type="submit" name="button" onclick="$(this).attr('disabled', 'disabled')"/>
Matt
  • 33,328
  • 25
  • 83
  • 97
  • Please consider editing your post to add more explanation about what your code does and why it will solve the problem. An answer that mostly just contains code (even if it's working) usually wont help the OP to understand their problem. – Drenmi Oct 30 '15 at 05:29
-1
document.getElementById('1').setAttribute('disabled', 'disabled');
Parv Sharma
  • 12,581
  • 4
  • 48
  • 80