Jump to content

Problem when enable button via checkbox. Need to click twice


gabrielpasv

Recommended Posts

Hi Guys, I am new to javascript and I am trying to enable / disable a button when checkbox is clicked. I am using a code I found online. However the problem is that I need to click twice on checkbox to enable the button. The button is a input type button. Please check my code

 

Can any of you please help me on this ? Thanks

    window.enabledisablenext = function ()    {        var checkboxes = $("input[type='checkbox']"),                submitButt = $("input[type='submit']");        checkboxes.click(function () {            submitButt.attr("disabled", !checkboxes.is(":checked"));        });    }
    <div class="row" style="text-align: center">        <input type='checkbox' onclick="enabledisablenext()"/>         <p> Aceito os termos do contato</p> <br>    </div>
        <form action="https://pagseguro.uol.com.br/checkout/v2/cart.html?action=add" method="post" onsubmit="PagSeguroLightbox(this);                return false;">            <input type="submit" class="btn btn-large btn-info" disabled value="Continuar Compra" alt="Pague com PagSeguro - é rápido, grátis e seguro!" />        </form>
Link to comment
Share on other sites

I don't think there's a need to invoke jQuery for such a trivial task.

 

For you need to update the HTML

<input type='checkbox' id="accept" />
<input type="submit" id="continue" class="btn btn-large btn-info" disabled value="Continuar Compra" alt="Pague com PagSeguro - é rápido, grátis e seguro!" />

Then a short script to make the checkbox change the button

document.getElementById("accept").onchange = function() {  // The "disabled" state is the opposite of the "checked" state, therefore use the [not] "!" operator  document.getElementById("continue").disabled = !this.checked;}

It looks like you're using XHTML, you probably should change your site to HTML 5 whenever you have the time

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...