Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi Team

I struggling to find a way in Jquery selectors ID unique, basically want to access one specific ID of my Button to work across all checkbox of mine. Is there a way to this in Jquery? I dont want my checkbox to download before one user select it, that what the code does at the moment. format of the file is correct its just the way of making this ID unique across all my checkbox for the button to see them, it seem its impossible to achieve it.

What I have tried:

JavaScript
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>
    

<pre lang="HTML"><!---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> 

 // checking for one field entry to read.
   $(document).ready(function() {
   $('#temperature').click(function() {
            var xcount = 0;
            if($('.temperature').prop('checked')) xcount--;
            if(xcount < 1) {
            window.location.href ='https://api.thingspeak.com/channels/899906/fields/1.csv';
            }
          });
  });
  
  // checking for two field entry to read.
  $(document).ready(function() {
    $('#download').click(function() {
      var ncount = 0;
      if($('.illuminance').prop('checked')) ncount--;
      if(ncount < 1) {
        window.location.href ='https://api.thingspeak.com/channels/899906/fields/2.csv';
      }
    });
  
  });
  
  // checking for third entry to read.
   $(document).ready(function() {
    $('#download').click(function() {
    var qcount = 0;
    if($('.button-state').prop('checked')) qcount--;
      if(qcount < 1) {
       window.location.href ='https://api.thingspeak.com/channels/899906/fields/8.csv';
      }
    });
  
  });
Posted
Updated 19-Nov-19 6:58am
v2
Comments
Herman<T>.Instance 19-Nov-19 6:36am    
I don't see 'temperature' in your HTML
gcogco10 19-Nov-19 7:05am    

Temperature
Afzaal Ahmad Zeeshan 19-Nov-19 9:42am    
He means to say, that your HTML does not contain an element with id temperature. Are you sure this is the complete code?

JavaScript
$('#temperature')

change it to
JavaScript
$("[id*=temperature]")
 
Share this answer
 
v2
Comments
gcogco10 19-Nov-19 7:18am    
Im still not getting it right, but if i am using that operator. which means i a pointing to all ID element throughout my document?
Herman<T>.Instance 20-Nov-19 4:23am    
You're getting confused between IDs and classes.
  • $("#foo") selects an element with id="foo";
  • $(".foo") selects an element with class="foo";

So your selector $('.temperature') will not match any elements, because there are no elements with that CSS class. You should be using $('#temperature') instead, as you have on the previous line.

But even then, it's not clear what you're trying to achieve. You start with a variable set to 0; if the checkbox is checked, you decrement that variable, making it -1; then, if the variable is less than 1, you perform some action. But the variable will always be less than 1!

And setting window.location.href will almost certainly stop any other scripts on the current page from running, so only one file will ever be downloaded.

Perhaps you want something like this?
JavaScript
$(function(){
    $("#download").click(function(e){
        e.preventDefault();
        
        if ($('#temperature').is(':checked')){
            window.open('https://api.thingspeak.com/channels/899906/fields/1.csv');
        }
        if ($('#illuminance').is(':checked')){
            window.open('https://api.thingspeak.com/channels/899906/fields/2.csv');
        }
        if ($('#button-state').is(':checked')){
            window.open('https://api.thingspeak.com/channels/899906/fields/8.csv');
        }
    });
});
 
Share this answer
 
Comments
gcogco10 20-Nov-19 2:16am    
Thanks mate, its working as per what i wanted to achieve
gcogco10 20-Nov-19 2:23am    
How do you check if there is a combination of either being selected? e.g if one is selected and other one is not and vice versa.
Richard Deeming 20-Nov-19 6:24am    
Not sure what you mean?
var temperatureIsChecked = $('#temperature').is(':checked');
var illuminanceIsChecked = $('#illuminance').is(':checked');
var buttonStateIsChecked = $('#button-state').is(':checked');
if (temperatureIsChecked && illuminanceIsChecked && buttonStateIsChecked) {
    // All three are checked
}
// etc.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900