0

I want to disable the button or change the attribute of the field so that if there are less than 12 characters in the input field it will disable the button I tried everything i know of.(commented) (Possible Duplicate of - Can't change html attribute with external script )

Html code

<button name="button" type="submit" id="continue_button" class="step__footer__continue-btn btn" aria-busy="false">
  <span class="btn__content" data-continue-button-content="true">Continue to shipping</span>
  <svg class="icon-svg icon-svg--size-18 btn__spinner icon-svg--spinner-button" aria-hidden="true" focusable="false"> <use xlink:href="#spinner-button"></use> </svg>
</button> 

JS code -

$("#checkout_shipping_address_address1").attr('maxlength','15');
      var valueLength = $('#checkout_shipping_address_address1').val();
      if(valueLength.length < 12){
     //   console.log(valueLength.length);
     //   $("#checkout_shipping_address_address1").aria-invalid="true";
       // var attrChange = $("#error-for-address1");
      //  $("#checkout_shipping_address_address1").innerHTML("aria-describedby" = attrChange);
       // $("#checkout_shipping_address_address1").innerHTML("aria-descibedby = attrChange"); 
     //   $("#checkout_shipping_address_address1").setAttribute("aria-invalid", "true");
      //  $('#continue_btn').attr("disabled", true);
      //  $('#continue_btn').disabled = true;
     //   $('#continue_btn').prop('disabled', true);
      }
      else
      {
       // $('#continue_btn').disabled=false;
      }

The attribute is not changing on the webpage neither can i disable the button.

Note- I cant change HTML/CSS code as i dont have access to it

P.S - I am quite new to JS/JQuery.

6
  • What is the HTML code of the field and button? Commented Feb 19, 2021 at 7:58
  • <button name="button" type="submit" id="continue_button" class="step__footer__continue-btn btn" aria-busy="false"><span class="btn__content" data-continue-button-content="true">Continue to shipping</span><svg class="icon-svg icon-svg--size-18 btn__spinner icon-svg--spinner-button" aria-hidden="true" focusable="false"> <use xlink:href="#spinner-button"></use> </svg></button> Commented Feb 19, 2021 at 8:00
  • 1
    The id of your button is id="continue_button", whilst in your JQuery you look for $('#continue_btn'), that's one reason it might not be working. Commented Feb 19, 2021 at 8:01
  • Thanks for the edit but it's still working, and in inspect i cant see the added/changed attribute.I used $('#continue_button').disabled= true; Commented Feb 19, 2021 at 8:06
  • 1
    you should be doing $('#continue_button').prop('disabled', true); Commented Feb 19, 2021 at 8:08

2 Answers 2

1

You have to put an event listener on your text input and I propose to you a vanilla JS solution :

  1. Disable button by default
  2. Check value length on input
  3. Change disabled value according to the result

const input = document.querySelector("#continue_input");
const button = document.querySelector("#continue_button");
input.addEventListener("input", (event) => {
  if(event.target.value.length > 11) {
    button.removeAttribute("disabled");
  } else {
    button.setAttribute("disabled", "true");
  }
});
.step__footer__continue-input {
  margin-bottom: 1rem;
}
<input id="continue_input" class="step__footer__continue-input" type="text" placeholder="Enter 12 characters here">
<button id="continue_button" class="step__footer__continue-btn btn" type="submit" name="button" aria-busy="false" disabled="true">
  <span class="btn__content" data-continue-button-content="true">Continue to shipping</span>
  <svg class="icon-svg icon-svg--size-18 btn__spinner icon-svg--spinner-button" aria-hidden="true" focusable="false"><use xlink:href="#spinner-button"></use></svg>
</button>

Sign up to request clarification or add additional context in comments.

Comments

0

The input needs an event handler function (added with on()) so that something happens when the user changes the input value:

$("#checkout_shipping_address_address1").attr('maxlength', '15').on('input', function() {
  $('#continue_button').prop('disabled', $(this).val().length < 12);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="checkout_shipping_address_address1" />
<button name="button" disabled type="submit" id="continue_button" class="step__footer__continue-btn btn" aria-busy="false">
  <span class="btn__content" data-continue-button-content="true">Continue to shipping</span>
  <svg class="icon-svg icon-svg--size-18 btn__spinner icon-svg--spinner-button" aria-hidden="true" focusable="false"> <use xlink:href="#spinner-button"></use> </svg>
</button>

1 Comment

Got your concept, but its not working for me.

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.