Search code examples
javascriptjqueryselectmaterialize

Read text of selected option [materialize]


i'm using materialize framework.

I'm trying to implement the materialize select: https://materializecss.com/select.html

and this is a sample code:

 <div class="input-field col s12">
    <select id="mySelect">
      <option value="" disabled selected>Choose your option</option>
      <option value="1"> text of option 1</option>
      <option value="2">text of option2</option>
      <option value="3"> text of option 3Option 3</option>
    </select>
    <label>Materialize Select</label>
  </div>

I'm trying to read the text of the selected option but I can't. Any suggestions?

the only thing I managed to take is the value of the option with this:

$("#MySelect").change(function() {
    console.log($('#MySelect').val());
    });

Solution

  • console.log( $("#MySelect option:selected").text() );