0

I have this problem.. I have this HTML code for 2 groups of checkbox:

    <label class="checkbox"><input type="checkbox" onClick="toggle_colors(this)" checked><i></i>All Colors</label>
<label class="checkbox"><input type="checkbox" name="colore" checked><i></i>red</label>
   <label class="checkbox"><input type="checkbox" name="colore" checked><i></i>yellow</label>

   <label class="checkbox"><input type="checkbox" onClick="toggle_brands(this)" checked><i></i>Tutte le Marche</label>
<label class="checkbox"><input type="checkbox" name="brand" checked><i></i>Acer</label>
   <label class="checkbox"><input type="checkbox" name="brand" checked><i></i>Asus</label>

And I use this javascripts for select all/toggle all:

function toggle_brands(source) {
  checkboxes = document.getElementsByName('brand');
  for(var i=0, n=checkboxes.length;i<n;i++) {
    checkboxes[i].checked = source.checked;
  }
}
function toggle_colors(source) {
  checkboxes = document.getElementsByName('colore');
  for(var i=0, n=checkboxes.length;i<n;i++) {
    checkboxes[i].checked = source.checked;
  }
}

When the user click on the checkbox I need to know if the checkbox is selected or not, to send the details of the query by AJAX.. To know if the checkbox I use this javascript:

    $( "[type=checkbox]" ).change(function() {
    if(this.checked) {
        alert('checked');
    }
    else {
        alert('unchecked');
    }
});

I want the text at the right of the selected checkbox..!

Ok.. With the parent work fine! I have added this code to create the query:

function create_query(){
var query = "SELECT * FROM computers ORDER BY " + ($("#ordine option:selected").val()) + " LIMIT " + ($("#risultati option:selected").val());
$.ajax({
    type: "POST",
    url: "static/cerca_prodotti.php",
    data: "query="+query,
    dataType: "html",
    success: function(risposta){
        $("div#risultati").html(risposta);
    },
})
}
$( "[type=checkbox]" ).change(function() {
    if(this.checked) {}
    else {}
});

$("#ordine").change(function() {
    create_query();
});

But I need to modify the query when the checkbox was checked/unchecked!

3
  • Please provide us your styles (css), because, by default without any style, there is text right of the checkbox. Please see this fiddle: jsfiddle.net/w2uhonqo Commented Feb 10, 2017 at 9:08
  • @freedomn-m your selector is a bit off mark, it should be $('[name="brand"]') Commented Feb 10, 2017 at 9:14
  • @ArunPJohny thanks - you're right - makes it even more confusing that they're mixed. I'll remove my comment anyway. Commented Feb 10, 2017 at 9:16

3 Answers 3

2

When you change the checked property programatically, the change event will not get triggered, you need to trigger the event manually.

$('input[data-all-type]').change(function() {
  $('input[name="' + $(this).data('allType') + '"]')[this.checked ? 'not' : 'filter'](':checked').prop('checked', this.checked).change();
})

$("input[type=checkbox]:not([data-all-type])").change(function() {
  if (this.checked) {
    console.log('checked', this);
  } else {
    console.log('unchecked', this);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="checkbox">
  <input type="checkbox" name="color-all" data-all-type="colore"><i></i>All Colors</label>
<label class="checkbox">
  <input type="checkbox" name="colore"><i></i>red</label>
<label class="checkbox">
  <input type="checkbox" name="colore"><i></i>yellow</label>

<label class="checkbox">
  <input type="checkbox" name="brand-all" data-all-type="brand"><i></i>Tutte le Marche</label>
<label class="checkbox">
  <input type="checkbox" name="brand"><i></i>Acer</label>
<label class="checkbox">
  <input type="checkbox" name="brand"><i></i>Asus</label>

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

Comments

0

You can do that with only one function

function toggle_checkbox(source, things) {
   $('input[name="' + things + '"]').prop('checked', $(source).is(':checked'))

   //OR

   $('input[name="' + things + '"]').prop('checked', source.checked);
}

function toggle_checkbox(source, things) {
  $('input[name="' + things + '"]').prop('checked', source.checked);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="checkbox">
  <input type="checkbox" onClick="toggle_checkbox(this,'colore')" checked><i></i>All Colors</label>
<label class="checkbox">
  <input type="checkbox" name="colore" checked><i></i>red</label>
<label class="checkbox">
  <input type="checkbox" name="colore" checked><i></i>yellow</label>

<label class="checkbox">
  <input type="checkbox" onClick="toggle_checkbox(this,'brand')" checked><i></i>Tutte le Marche</label>
<label class="checkbox">
  <input type="checkbox" name="brand" checked><i></i>Acer</label>
<label class="checkbox">
  <input type="checkbox" name="brand" checked><i></i>Asus</label>

Comments

0

You can get text of the parent node of the checkbox in this way:

$( "[type=checkbox]" ).change(function() {
   var text = this.parentElement.innerText;
    if(this.checked) {
        alert(text + ': checked');
    }
    else {
        alert(text + ': unchecked');
    }
});

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.