Search code examples
javascriptjqueryicheck

jQuery i-Check plugin same class name - retrieving two different values


I'm using I-check plugin and I'm trying to retrieve the value from two input fields that represents two different fields.

Html:

<label><input id="hoteltype" class="i-check" type="checkbox" name="hotel" value="3"/>Hotel</label>

 <label><input id="starrating" class="i-check" type="checkbox" name="1" value="1.0" />1 star</label>

jQuery.ajax

<script>
            $('.i-check input').on('ifChanged', function(event){
                $('.searchtable').addClass('hide');
                $('.spinner').removeClass('hide');

                $.ajax({
                    type: 'GET',
                    data: {'name':'<?php echo strval($_GET['name']); ?>','arrival':'<?php echo strval($_GET['arrival']); ?>','departure':'<?php echo   strval($_GET['departure']);?>','guests':'<?php echo strval($_GET['guests']);?>','propertyCategory':$(".i-check input[type='checkbox']:checked").val(),'minStarRating':$(".i-check input[type='checkbox']:checked").val(),'order_by':$('#order_by').val()},
                    url: '<?php echo $baseUrl ?>/hotels/hotelFilterResult.php',


                    success: function (data) {
                    //alert('data loaded succesfully');
                        alert(this.url);

                        $('.searchtable').replaceWith(data);
                        $('.spinner').addClass('hide');
                        $('.searchtable').removeClass('hide');

                    },
                    error: function (xhr) {
                        alert('data not loaded');
                        }
                    });

           });

The issue is not to retrieve the value, the problem is to separate the values, as I cannot change the classname.

I retrive the value by

'minStarRating':$(".i-check input[type='checkbox']:checked").val()
'hoteltype':$(".i-check input[type='checkbox']:checked").val()

The problem is now that I get the same value in two different representations.

So e.g if star rating is selected - value I get here is in both 1.0, but really it should be empty in hoteltype. The issue lies within the identifier ".i-check". I have tried to add an ID class to both so one has id="starrating" and the other id="hoteltype" and then do it like this:

 'minStarRating':$(".i-check#starrating input[type='checkbox']:checked").val()
'hoteltype':$(".i-check#hoteltype input[type='checkbox']:checked").val()

This dosen't seem to work with I-Check plugin, how can I solve this so I can get the two values separated?


Solution

  • If you leave a space between the id and the element name which in this case is input, the browser thinks that the input element is a descendant of the id element; so I think it's better to leave out spaces between those two. More info about ID selectors. Below are some alternatives that can be used to find the values in question.

    $("label").on("click", "input:checkbox", function() {
      
        var result =   'minStarRating ' + $("input[type='checkbox']:checked#starrating").val();
       var result2 =  'hoteltype ' + $("input[type='checkbox']:checked#hoteltype").val();
        
      // same as above but in different order
     // var result =   'minStarRating ' + $("#starrating:input[type='checkbox']:checked").val();
    // var result2 =  'hoteltype ' + $("#hoteltype:input[type='checkbox']:checked").val();
      
      //the below works too but it's longer, it uses also class names
    //var result =   'minStarRating ' + $(".i-check#starrating:input[type='checkbox']:checked").val();
    //var result2 =  'hoteltype ' + $(".i-check#hoteltype:input[type='checkbox']:checked").val();
      
      $("#result").text(result);
      
      $("#result2").text(result2);
      
    
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <label>
      <input id="hoteltype" class="i-check" type="checkbox" name="hotel" value="3" />Hotel</label>
    
    <label>
      <input id="starrating" class="i-check" type="checkbox" name="1" value="1.0" />1 star</label>
    
    
    <div id="result"></div>
    
    <div id="result2"></div>

    I'm not sure did I answer your question because I haven't used iCheck before (although it looks very interesting plugin with great style), but I hope this helps a bit :-)