Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: High
  • Security: Public
  • Views: 85
  • Last Modified:

Change Select Selected in Modal

Hi E's,
I Have 3 buttons and a Materialize Modal that contains a HTML Select with Four options to choose from. 0 by default and 1, 2, and 3, and each number between 1 to 3 are associated to each button.
The objective is, when I click in any button, open the Modal and change the select view for the correspondent number.
I try change the selected with jQuery in this way:
$("#numbers[value=3]").attr("selected", "selected");
but not work.
In attach image you can see what appear regardless of which button I click.Modal view
<!DOCTYPE html>
 <html>
   <head>
     <!--Import Google Icon Font-->
     <link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
     <!--Import materialize.css-->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.1/css/materialize.min.css">
     <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
   </head>
   <body>
<a class="waves-effect waves-light btn b1">button one</a>
<a class="waves-effect waves-light btn b2">button two</a>
<a class="waves-effect waves-light btn b3">button 333</a>

<!-- Modal Structure -->
  <div id="modal1" class="modal">
    <div class="modal-content">
      <h4>Header</h4>
  <div class="input-field col s12">
  <select id="numbers">
    <option value="0" selected>Choose your number</option>
    <option value="1">one</option>
    <option value="2">two</option>
    <option value="3">3333</option>
  </select>
  <label>Materialize Select</label>
</div>
    </div>
    <div class="modal-footer">
      <a href="#!" class="modal-action modal-close waves-effect waves-green btn-flat">Send</a>
    </div>
  </div>

     <!--Import jQuery before materialize.js-->
     <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.1/js/materialize.min.js"></script>

<script>
$('.modal').modal();
$('select').material_select();
</script>

<script>
$(".b1").click(function(){
$('#modal1').modal('open');
$("#numbers[value=1]").attr("selected", "selected");
});

$(".b2").click(function(){
$('#modal1').modal('open');
$("#numbers[value=2]").attr("selected", "selected");
});

$(".b3").click(function(){
$('#modal1').modal('open');
$("#numbers[value=3]").attr("selected", "selected");
});
</script>
   </body>
 </html>

Open in new window

The best regards, JC
0
Pedro Chagas
Asked:
Pedro Chagas
  • 3
2 Solutions
 
Julian HansenCommented:
Just do
$('#numbers').val(3);

Open in new window

0
 
Julian HansenCommented:
You will need to re-initialise the material_select after doing this
<script>
$('.modal').modal();
$('select').material_select();
</script>

<script>
$(".b1").click(function(){
$('#modal1').modal('open');
$("#numbers").val(1);
$("#numbers").material_select();
});

$(".b2").click(function(){
$('#modal1').modal('open');
$("#numbers").val(2);
$("#numbers").material_select();
});

$(".b3").click(function(){
$('#modal1').modal('open');
$("#numbers").val(3);
$("#numbers").material_select();
});
</script>

Open in new window

Working sample here
1
 
Pedro ChagasWebmasterAuthor Commented:
Thank you.
0
 
Julian HansenCommented:
You are welcome.
1

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now