title: How to select an element jQuery by value
body: <p>I want to change the property checked of my checkbox if it's value is equal an other value , so here is my essay:</p>
  
  <p>I work with symfony and AJAX so I have two twig , in the first I have the code AJAX lead to call the second :
  tousContacts.html.twig</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>{% extends '::basecontent.html.twig' %}
  
      {% block title %}
          {{ page_title }}
      {% endblock %}
  
  {% block stylesheets %}
      &lt;link href="{{ asset('assets/apps/css/todo-2.min.css') }}" rel="stylesheet" type="text/css"&gt;
      &lt;link href="{{ asset('assets/global/plugins/select2/css/select2.min.css') }}" rel="stylesheet" type="text/css"/&gt;
      &lt;link href="{{ asset('assets/global/plugins/select2/css/select2-bootstrap.min.css') }}" rel="stylesheet" type="text/css"/&gt;
  {% endblock %}
  
  {% block content %}
  
      &lt;div class="portlet light bordered"&gt;
          &lt;div class="portlet-title"&gt;
              &lt;div class="caption"&gt;
                  &lt;i class="icon-user font-green"&gt;&lt;/i&gt;
                  &lt;span class="caption-subject font-green sbold uppercase"&gt;{{ page_title }}&lt;/span&gt;
              &lt;/div&gt;
              &lt;div class="actions"&gt;
                  &lt;div class="btn-group btn-group-devided"&gt;
                      {{ form_start(formliste, {'attr': {'id': 'form_env', 'class': 'form_remplirliste'}}) }}
                          &lt;div class="input-group"&gt;
                              &lt;div class="input-icon"&gt;
                                  {{ form_row(formliste.libelle, {'attr': {'class': 'form-control'} }) }}
                              &lt;/div&gt;
                              &lt;div class="input-group-btn"&gt;
                                  &lt;button id="genpassword" class="btn btn-success" type="submit" id="form_save"&gt;
                                      &lt;i class="fa fa-arrow-left fa-fw"/&gt;&lt;/i&gt; Créer la liste
                                  &lt;/button&gt;
                              &lt;/div&gt;
                              &lt;div class="input-group-btn"&gt;
                                  &lt;a href="{{ path('liste_showEvListe', {'id': id }) }}" class="btn btn-outline green"&gt;&lt;i class="fa fa-list"&gt;&lt;/i&gt; Terminer&lt;/a&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      {{ form_end(formliste) }}&lt;br&gt;
  
                      &lt;input type="hidden" id="myHidden" value="0"/&gt;
  
                      &lt;div class="modal-b"&gt;
                          &lt;img src="{{ asset('assets/global/img/loading-spinner-grey.gif') }}" alt="" class="loading"&gt;
                          &lt;span&gt; &amp;nbsp;&amp;nbsp;Chargement... &lt;/span&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          {% form_theme form 'bootstrap_3_layout.html.twig' %}
          &lt;div class="portlet-body form"&gt;
              &lt;div class="todo-ui"&gt;
                  &lt;div class="todo-sidebar"&gt;
                      &lt;div class="portlet light " style="margin-bottom: 0"&gt;
                          &lt;div class="portlet-title"&gt;
                              &lt;div class="caption" data-toggle="collapse" data-target=".todo-project-list-content"&gt;
                                  &lt;span class="caption-subject font-green-sharp bold uppercase"&gt;Recherche par &lt;/span&gt;
                                  &lt;span class="caption-helper visible-sm-inline-block visible-xs-inline-block"&gt;click to view project list&lt;/span&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                          &lt;div class="portlet-body todo-project-list-content" style="height: auto;"&gt;
                              &lt;div class="todo-project-list"&gt;
                                  &lt;div class="form-group"&gt;
                                      {{ form_widget(form.search) }}
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;div class="portlet light " style="margin-bottom: 0"&gt;
                          &lt;div class="portlet-title"&gt;
                              &lt;div class="caption" data-toggle="collapse" data-target=".todo-project-list-content2"&gt;
                                  &lt;span class="caption-subject font-green-sharp bold uppercase"&gt;Organismes &lt;/span&gt;
                                  &lt;span class="caption-helper visible-sm-inline-block visible-xs-inline-block"&gt;click to view project list&lt;/span&gt;
                              &lt;/div&gt;
                              &lt;div class="actions"&gt;
                                  &lt;div class="actions"&gt;
                                      &lt;a class="btn btn-circle grey-salsa btn-outline btn-sm add_item" href="javascript:;"&gt;
                                          &lt;i class="fa fa-plus"&gt;&lt;/i&gt; &lt;/a&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                          &lt;div class="portlet-body todo-project-list-content2" style="height: auto;"&gt;
                              &lt;div class="todo-project-list"&gt;
                                  &lt;ul class="nav nav-stacked"&gt;
                                      {# &lt;li class="no_item"&gt;&lt;a href="javascript:;"&gt; Aucun organisme spécifié &lt;/a&gt;&lt;/li&gt; #}
                                      &lt;li class="add_item"&gt;{{ form_widget(form.organismes) }}&lt;/li&gt;
                                  &lt;/ul&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;div class="portlet light " style="margin-bottom: 0"&gt;
                          &lt;div class="portlet-title"&gt;
                              &lt;div class="caption" data-toggle="collapse" data-target=".todo-project-list-content-tags3"&gt;
                                  &lt;span class="caption-subject font-red bold uppercase"&gt;Programmes &lt;/span&gt;
                                  &lt;span class="caption-helper visible-sm-inline-block visible-xs-inline-block"&gt;click to view&lt;/span&gt;
                              &lt;/div&gt;
                              &lt;div class="actions"&gt;
                                  &lt;div class="actions"&gt;
                                      &lt;a class="btn btn-circle grey-salsa btn-outline btn-sm add_item" href="javascript:;"&gt;
                                          &lt;i class="fa fa-plus"&gt;&lt;/i&gt; &lt;/a&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                          &lt;div class="portlet-body todo-project-list-content todo-project-list-content-tags3" style="height: auto;"&gt;
                              &lt;div class="todo-project-list"&gt;
                                  &lt;ul class="nav nav-pills nav-stacked"&gt;
                                      {# &lt;li class="no_item"&gt;&lt;a href="javascript:;"&gt; Aucun programme spécifié &lt;/a&gt;&lt;/li&gt; #}
                                      &lt;li class="add_item"&gt;{{ form_widget(form.programmes) }}&lt;/li&gt;
                                  &lt;/ul&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;div class="portlet light " style="margin-bottom: 0"&gt;
                          &lt;div class="portlet-title"&gt;
                              &lt;div class="caption" data-toggle="collapse" data-target=".todo-project-list-content-tags3"&gt;
                                  &lt;span class="caption-subject font-red bold uppercase"&gt; Catégorie &lt;/span&gt;
                                  &lt;span class="caption-helper visible-sm-inline-block visible-xs-inline-block"&gt;click to view&lt;/span&gt;
                              &lt;/div&gt;
                              &lt;div class="actions"&gt;
                                  &lt;div class="actions"&gt;
                                      &lt;a class="btn btn-circle grey-salsa btn-outline btn-sm add_item" href="javascript:;"&gt;
                                          &lt;i class="fa fa-plus"&gt;&lt;/i&gt; &lt;/a&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                          &lt;div class="portlet-body todo-project-list-content todo-project-list-content-tags3" style="height: auto;"&gt;
                              &lt;div class="todo-project-list"&gt;
                                  &lt;ul class="nav nav-pills nav-stacked"&gt;
                                      {# &lt;li class="no_item"&gt;&lt;a href="javascript:;"&gt; Aucune catégorie spécifiée &lt;/a&gt;&lt;/li&gt; #}
                                      &lt;li class="add_item"&gt;{{ form_widget(form.categories) }}&lt;/li&gt;
                                  &lt;/ul&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;!-- END TODO SIDEBAR --&gt;
                  &lt;!-- BEGIN TODO CONTENT --&gt;
                  &lt;div class="todo-content"&gt;
                      &lt;div class="portlet light "&gt;
                          &lt;div class="portlet-body"&gt;
                              &lt;div class="row"&gt;
                                  &lt;div class="todo-tasklist contacts"&gt;
                                      &lt;div class="col-md-12"&gt;
                                          &lt;p class="todo-tasklist-date"&gt;Pas de résultats trouvés&lt;/p&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;!-- END TODO CONTENT --&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div class="modal fade" id="ajax" role="basic" aria-hidden="true"&gt;
          &lt;div class="modal-dialog"&gt;
              &lt;div class="modal-content"&gt;
                  &lt;div class="modal-body"&gt;
                      &lt;img src="{{ asset('assets/global/img/loading-spinner-grey.gif') }}" alt="" class="loading"&gt; &lt;span&gt; &amp;nbsp;&amp;nbsp;Chargement... &lt;/span&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  {% endblock %}
  
  {% block javascripts %}
      &lt;script src="{{ asset('assets/global/plugins/select2/js/select2.full.min.js') }}" type="text/javascript"&gt;&lt;/script&gt;
      &lt;script type="application/javascript"&gt;
          $(".modal-b").hide();
          // var data =new Array();
          var list = [];
          var listno = [];
          $(document).ready(function () {
              $('.menu-toggler.sidebar-toggler').trigger('click');
  
           
          });
          url = '{{ path('liste_contacts') }}';
          var query = {
              'search': '',
              'page': 1,
              'organismes': [],
              'programmes': [],
              'categories': []
          };
          updateListeContacts();
          function updateListeContacts() {
              var ws = url;
              ws += '?key_search=' + query['search'];
              ws += '&amp;organismes_search=' + query['organismes'];
              ws += '&amp;programmes_search=' + query['programmes'];
              ws += '&amp;categories_search=' + query['categories'];
              ws += '&amp;page=' + query['page'];
              $.get(ws, function (data) {
                  $('.todo-tasklist&gt;div').remove();
                  $('.todo-tasklist').html(data);
                  set_favaction_listner();
                  set_paginator_listner();
                  set_checkction_listner();
              });
          }
          $('li.add_item').hide();
          $('a.add_item').click(function (e) {
              e.preventDefault();
              var $this = $(this);
              if ($this.hasClass('add_item'))
                  $(this).removeClass('add_item').addClass('close_item').find('i').removeClass('fa-plus').addClass('fa-close').parent().parent().parent().parent().parent().find('li.add_item').show();
              else if ($this.hasClass('close_item'))
                  $(this).removeClass('close_item').addClass('add_item').find('i').removeClass('fa-close').addClass('fa-plus').parent().parent().parent().parent().parent().find('li.add_item').hide();
          });
          $("select[name='form[organismes]'],select[name='form[programmes]'],select[name='form[categories]']").prepend('&lt;option value="0" selected disabled&gt;choisir&lt;/option&gt;');
          $("select[name='form[organismes]'],select[name='form[programmes]'],select[name='form[categories]']").select2({
              allowClear: true,
              placeholder: 'TEst',
              width: null
          }).on("change", function () {
              query['page'] = 1;
              var $this = $(this);
              var $container = $this.parent().parent();
              query[$this.attr('name').replace('form[', '').replace(']', '')].push($this.val());
              $container.find('li.no_item').hide();
              var $selectedItem = $this.find("option[value=" + $this.val() + "]");
              var $deleteIcon = $('&lt;span class="badge badge-danger" data-id="' + $this.val() + '"&gt;&lt;i class="fa fa-close"&gt;&lt;/i&gt;&lt;/span&gt;');
              $deleteIcon.click(function () {
                  removeSearch($(this));
              });
              $selectedItem.remove();
              $this.val(0);
              $this.trigger('change.select2');
              $container.parent().parent().parent().find('.close_item').trigger('click');
              var $li = $('&lt;li /&gt;');
              var $a = $('&lt;a href="javascript:;" /&gt;');
              var $span = $('&lt;span class="badge badge-success"&gt; 0 &lt;/span&gt;');
              var $spanTxt = $('&lt;span class="text"&gt;' + $selectedItem.text() + '&lt;/span&gt;');
              $a.append($spanTxt);
              $a.prepend($deleteIcon);
              $a.prepend($span);
              $li.append($a);
              $container.append($li)
              updateListeContacts();
          });
          var typingTimer;                //timer identifier
          var doneTypingInterval = 500;  //time in ms, 5 second for example
          var $input = $("input[name='form[search]']");
          //on keyup, start the countdown
          $input.on('keyup', function () {
              clearTimeout(typingTimer);
              typingTimer = setTimeout(doneTyping, doneTypingInterval);
          });
          //on keydown, clear the countdown
          $input.on('keydown', function () {
              clearTimeout(typingTimer);
          });
          //user is "finished typing," do something
          function doneTyping() {
              query['search'] = $input.val();
              query['page'] = 1;
              updateListeContacts();
          }
          function removeSearch($this) {
              $container = $this.parent().parent().parent();
              queryCible = query[$container.find('select').attr('name').replace('form[', '').replace(']', '')];
              query[$container.find('select').attr('name').replace('form[', '').replace(']', '')].splice(queryCible.indexOf($this.attr('data-id')), 1);
              //delete query[$container.find('select').attr('name').replace('form[','').replace(']','')][queryCible.indexOf($this.attr('data-id'))];
              $container.find('select').append('&lt;option value="' + $this.attr('data-id') + '"&gt;' + $this.parent().find('.text').text() + '&lt;/option&gt;');
              $this.parent().parent().remove();
              if ($container.find('li').length &lt; 3) {
                  $container.find('li.no_item').show();
              }
              updateListeContacts();
          }
          function set_paginator_listner() {
              $('.paginator button:not(.active)').click(function (ev) {
                  ev.preventDefault();
                  query['page'] = $(this).attr('data-page');
                  updateListeContacts();
              });
          }
          function set_favaction_listner() {
              $('.set_fave').bind('click', function () {
                  $this = $(this);
                  $.get($(this).attr('data-url'), function (data) {
                      if (data.toString() == 'true') {
                          $this.parent().parent().find('.set_fave i').removeClass('fa-star-o').addClass('fa-star');
                      } else {
                          $this.parent().parent().find('.set_fave i').removeClass('fa-star').addClass('fa-star-o');
                      }
                  });
              });
          }
          set_favaction_listner();
          function set_checkction_listner() {
              //list=$("input:checked").map(function () { return this.value; }).get();
                       
                 list = $.merge($("input:checked").map(function () {
                  return this.value;
              }).get(), list);
              list = unique(list);
              var all= $("input:checkbox").map(function () { return this.value; }).get();
              var i;
                  for (i = 0; i &lt; all.length; ++i) {
                   for (j = 0; j &lt; list.length; ++j) {
                   if(all[i]==list[j]){
                    
                     var a = all[i];
                   console.log($('.checkbox[value = a]').val());
                   //$('.checkbox[value = a]').attr('checked','checked');
                   }
                  }
                  }
                     
              $('.checkbox').bind('change', function () {
                  //var x = $("input:checkbox:checked").val();
                  // $this = $(this);
                  var x = $(this).val();
                  if ($(this).is(':checked')) {
                      console.log(x);
                      list.push(x);
                      console.log(list);
                  } else {
                      console.log(x);
                      list = jQuery.grep(list, function (value) {
                          return value != x;
                      });
                      console.log(list);
                  }
              });
          }
          set_checkction_listner();
          function unique(list) {
              var result = [];
              $.each(list, function (i, e) {
                  if ($.inArray(e, result) == -1) result.push(e);
              });
              return result;
          }
          $("#form_env").submit(function (ev) {
              ev.preventDefault();
              $(".modal-b").show();
              var x = $("#form_libelle").val();
              var id = {{id}};
              var hidden = $("#myHidden").val();
              $.ajax({
                  type: 'post',
                  url: '{{ path('enregistrer22_liste') }}',
                  data: {
                      arrayy: list,
                      x: x,
                      ev: id,
                      hidden: hidden,
                  },
                  beforeSend: function () {
                  },
                  success: function (data) {
                      $(".modal-b").hide();
                      $("#myHidden").val(data['idliste']);
                      $("#form_save").attr('value', "modifier la liste");
                      //console.log(data['idliste']);
                  }
              });
          });
      &lt;/script&gt;
  {% endblock %}</code></pre>
  </div>
  </div>
  </p>
  
  <p>the second twig is just the list of contact with a checkbox 
  assigned to each contact</p>