-2

I have created 3 checkbox and i am struggling to make them to communicate with my Jquery button i have created to allow a user an option to select. If one is selected and other one both are, they must when button is clicked should download my feeds as CSV file. I have the logic below i want to share for clear understanding as to what exactly i want to achieve.

$(document).ready(function() {
  $("#download").click(function() {
    window.location.href = 'https://api.thingspeak.com/channels/899906/feeds.csv?start=2019-11-12%2019:11:12&end=2019-11-13%2019:11:13';
  });

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="custom-control custom-checkbox">
  <input type="checkbox" class="custom-control-input" id="temperature">
  <label class="custom-control-label" for="temperature">Temperature</label>
</div>

<div class="custom-control custom-checkbox">
  <input type="checkbox" class="custom-control-input" id="illuminance">
  <label class="custom-control-label" for="illuminance">Illuminance</label>
</div>
<div class="custom-control custom-checkbox">
  <input type="checkbox" class="custom-control-input" id="button-state">
  <label class="custom-control-label" for="button-state">Button-State</label>

  <!---Downloading File using 
    Jquery with Buttons---->
  <div class="form-group"><br>
    <div class="col-md-1.9 text-center">
      <button id="download" name="download" class="btn btn-warning">Download</button><br>
    </div>
  </div>
prasanth
  • 22,145
  • 4
  • 29
  • 53
Gcobza
  • 432
  • 1
  • 5
  • 12
  • What have you tried so far? I see no code to trigger any button action – Nico Haase Nov 15 '19 at 08:31
  • Well on Jquery, i am calling an attribute ID for downloading file using REST on window.location.href. Is there way to do this better? i am struggling to apply something similar to my checkbox as well – Gcobza Nov 15 '19 at 08:38
  • Your button click (via user click) and csv download via window.location.href are fine. We don't understand where the checkboxes come in - do they need to be checked when you click? Or when you check them should it auto-click the button? – freedomn-m Nov 15 '19 at 08:40
  • This might help: https://stackoverflow.com/questions/901712/how-to-check-whether-a-checkbox-is-checked-in-jquery – freedomn-m Nov 15 '19 at 08:40

1 Answers1

0

You want to initiate the download if at least two checkboxes are checked?

In that case, try using this:

$(document).ready(function() {
    $("#download").click(function() {
        var count = 0;
        if ($('#temperature').prop('checked')) count++;
        if ($('#illuminance').prop('checked')) count++;
        if ($('#button-state').prop('checked')) count++;
        if (count > 1) {
            window.location.href ='https://api.thingspeak.com/channels/899906/feeds.csv?start=2019-11-12%2019:11:12&end=2019-11-13%2019:11:13';
        }
    });
});
yellowtail
  • 435
  • 3
  • 12
  • Yes i want to initiate the download if the 2 checkboxes are checked – Gcobza Nov 15 '19 at 08:43
  • How do i then, if 2 checkbox are checked be shown on my download CSV, currently now my download just download all feeds(including button-state etc)? Is there a work around to this issue? – Gcobza Nov 15 '19 at 09:17