0

I'm trying to change the button from disabled to enabled when both data-attributes are NOT empty.

I dont get any errors but everytime I fill the data-attributes its not changing its disabled/enabled attribute.

I've tried it like this but nothing is happening, my browsers console isnt printing out anything.

<select class="form-control os" name="os">
  <option disabled selected>Please choose...</option>
  <option>Ubuntu Server 18.04</option>
  <option>Ubuntu Server 16.04</option>
  <option>Ubuntu Server 14.04</option>
</select>

<select class="form-control location" name="location">
  <option value="" disabled selected>Please choose...</option>
  <option value="Sydney">Sydney</option>
  <option value="Tokyo">Tokyo</option>
  <option value="Sao Paulo">São Paulo</option>
</select>

<button class="btn btn-primary btn-lg float-right btn-buynow" data-shoppy-product="ewAfime" data-shoppy-os="" data-shoppy-location="" disabled>Buy now</button>
$(document).ready(function(){
    $("select.location").change(function(){
        var selectedLocation = $(this).children("option:selected").val();
        $('.btn-buynow').attr('data-shoppy-location', selectedLocation);
    });
    $('select.os').change(function() {
        var selectedOS = $(this).children("option:selected").val();
        $('.btn-buynow').attr('data-shoppy-os', selectedOS);
    });

    var $buynow = $('.btn-buynow');
    if (typeof $buynow.data('shoppy-os') !== 'undefined' && typeof $buynow.data('shoppy-location') !== 'undefined') {
      if($buynow.attr('disabled')) $buynow.removeAttr('disabled');
      else $buynow.attr('disabled', 'disabled');
    }
});
4
  • And why should it? You're not logging anything to the console. Commented May 8, 2019 at 10:19
  • Sorry, I'm quite unfamiliar with jquery/javascript, I've tried to log to the console with console.log(); but its not printing anything Commented May 8, 2019 at 10:25
  • Should you instead be checking if the data attribute exists? What you are doing only returns true if the data attribute is not an empty string, or if the value is 0 or false. To check for the actual existence of the data attribute you should use typeof($buynow).data(“data-shoppy-os”) !== “undefined” Commented May 8, 2019 at 10:36
  • Please post your (relevant, minimal reproducible example) HTML in your question as well in order that we can see what you're doing, and his to reproduce the problem. Linking to a JS Fiddle is a useful extra, but your Friday coffee must be in your question. Commented May 8, 2019 at 10:48

2 Answers 2

1

Try following

  • While accessing data attributes using .data(), strip the data- from the argument
  • To disable a button, use .attr('disabled', 'disabled');

var $buynow = $('.btn-buynow');
if ($buynow.data('shoppy-os') && $buynow.data('shoppy-location')) {
  if($buynow.attr('disabled')) $buynow.removeAttr('disabled');
  else $buynow.attr('disabled', 'disabled');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button type="button" class="btn-buynow" data-shoppy-os="a" data-shoppy-location="b" disabled>Button</button>

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

8 Comments

Thanks for your answer. This isnt working for me though. I'm getting the data value from two different dropdown menus, should I add it into the change event?
Can you please share your code snippet for better understanding of the problem? Additionally, if you are changing values and based on that you want the button to be enabled/disabled then you need to have change event
This isnt working correctly. When I choose from the first dropdown the button already gets enabled.
What is the desired behavior?
It should be enabled when both data-attributes have values.
|
0

I would suggest a few changes for my method of validation to work. They are as follows:

HTML Changes: adding values to HTML options

<select class="form-control os" name="os">
  <option value="Please choose..." selected>Please choose...</option>
  <option value="Ubuntu Server 18.04">Ubuntu Server 18.04</option>
  <option value="Ubuntu Server 16.04">Ubuntu Server 16.04</option>
  <option value="Ubuntu Server 14.04">Ubuntu Server 14.04</option>
</select>

<select class="form-control location" name="location">
<option value="Please choose..." selected>Please choose...</option>  <option value="Sydney">Sydney</option>
  <option value="Tokyo">Tokyo</option>
  <option value="Sao Paulo">São Paulo</option>
</select>

<button class="btn btn-primary btn-lg float-right btn-buynow" data-shoppy-product="ewAfime" data-shoppy-os="" data-shoppy-location="" disabled>Buy now</button>

And the JS Validation is as follows:

$(".os,.location").change(function() {
    if ($(".os").val() != "Please choose..." && $(".location").val() != "Please choose...") {
      $('.btn-buynow').prop("disabled", false);
    }
  });

Let me know if this works out for you.

Comments

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.