Set <option> when checking/unchecking checkbox

Asked

Viewed 280 times

0

I need to reset a select through a checkbox. The problem is the moment I try to access option with jQuery.

HTML

<select id="selSub">
 <option value="0" >Selecione</option>
 <option value="1" >1</option>
 <option value="2" >2</option>
</select>

jQuery

$(document).ready(function(){
  var checkBox = $("#c1").prop('checked');
  $('#divSubIn').hide();
  $("#c1").click(function(){
    if($('#c1').is(":checked")) {
      //IF CHECKED      
      $('#label1').text("insira uma nova >>");
      $('#divSubSel').hide();
      $('#divSubIn').show();
      $('#selSub').material_select('selectedIndex', 0);
    }else{
      //IF UNCHECKED
      $('#label1').text("Inserir subcategoria nova");
      $('#divSubSel').show();
      $('#divSubIn').hide();      
    }
  });  
});

Within the // IF CHECKED the fourth element $('#selSub').material_select('selectedIndex', 0); should return select to normal = <option value="0" >Selecione</option>.

  • If you just want to reset select. Just exchange: "$('#selSub'). material_select('selectedIndex', 0);" for: "$('#selSub'). val('0');"

1 answer

0


  • Paul, did not work, select does not return to select after clicking the checkbox, materialize uses a script to start select, the problem must be there...

  • WHAT IS MATERIALIZE?

  • A framework, developed by Google, based on material design: http://materializecss.com/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.