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>

7 Answers 7

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>
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

prop() should be used here instead of attr(). See: prop vs attr. For changing state, prop is appropriate.
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);
});

Comments

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.

Comments

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

1 Comment

prop() should be used here instead of attr(). See: prop vs attr. For changing state, prop is appropriate.
0
<input type="submit" name="button" onclick="$(this).attr('disabled', 'disabled')"/>

1 Comment

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.
-1
document.getElementById('1').setAttribute('disabled', 'disabled');

1 Comment

Element IDs cannot begin with a number ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.