Narxx
Narxx

Reputation: 8299

Materialize CSS - Select does not have name nor proper value in DOM

I'm having trouble understanding how materialize CSS <select> tag works.

In regular HTML select tags, you'd insert a name="" attribute, and for each option, a value="" attribute, which seem to be missing in materialize CSS.

This code:

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

renders into this in DOM:

<div class="input-field col s12 m6">
  <div class="select-wrapper"><span class="caret">▼</span>
    <input class="select-dropdown" readonly="true" data-activates="select-options-273ec07d-e7c4-e689-e2e3-6a57ff2f6293" value="Choose your option" type="text">
    <ul style="width: 296px; position: absolute; top: 0px; left: 0px; opacity: 1; display: none;" id="select-options-273ec07d-e7c4-e689-e2e3-6a57ff2f6293" class="dropdown-content select-dropdown">
      <li class="disabled"><span>Choose your option</span></li><li class="active selected"><span>Option 1</span></li><li class=""><span>Option 2</span></li>
      <li class=""><span>Option 3</span></li>
    </ul>
    <select class="initialized">
          <option value="" disabled="" selected="">Choose your option</option>
          <option value="1">Option 1</option>
          <option value="2">Option 2</option>
          <option value="3">Option 3</option>
    </select>
  </div>
  <label>Materialize Select</label>
</div>

Problems I am having:

  1. Where and how do I set the name for that select element (to be represented accordingly in the rendered input element)?
  2. The values are the texts being displayed in the list. I wish to have different values than the display strings, e.g:

<option value="1">regular style</option> <option value="2">bold style</option> <option value="3">italic style</option>

Thanks all in advance :)

Upvotes: 2

Views: 1313

Answers (2)

Gerardo Verrone
Gerardo Verrone

Reputation: 431

Let me show you an example to add name to a materialize css select

If your html indicates this

<div class="col s3">
    <span class="required"></span>
    <select id="piezas" name="piezas" class="validate">
        <option value="-1" disabled selected>Elija Pieza</option>
        <option value="1">Tornillo</option>
        <option value="2">Clavo</option>
        <option value="3">Cable</option>
    </select>
</div> 

Your select will be transformed and shown in the dom to something like this

<div class="col s3">
    <span class="required"></span>
    <div class="select-wrapper valid">
        
        <input class="select-dropdown dropdown-trigger" type="text" readonly="true" data-target="select-options-9e9993fe-936f-86eb-22f5-ec638cf0121d" name="piezas-materialize" aria-invalid="false">
        
        <ul id="select-options-9e9993fe-936f-86eb-22f5-ec638cf0121d" class="dropdown-content select-dropdown" tabindex="0">
            <li class="disabled selected" id="select-options-9e9993fe-936f-86eb-22f5-ec638cf0121d0" tabindex="0">
                <span>Elija Pieza</span>
            </li>
            <li id="select-options-9e9993fe-936f-86eb-22f5-ec638cf0121d1" tabindex="0">
                <span>Tornillo</span>
            </li>
            <li id="select-options-9e9993fe-936f-86eb-22f5-ec638cf0121d2" tabindex="0">
                <span>Clavo</span></li>
            <li id="select-options-9e9993fe-936f-86eb-22f5-ec638cf0121d3" tabindex="0">
                <span>Cable</span>
            </li>
        </ul>

        <svg class="caret" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
            <path d="M7 10l5 5 5-5z"></path><path d="M0 0h24v24H0z" fill="none"></path>
        </svg>

        <select id="piezas" name="piezas" class="validate ignore" tabindex="-1">
            <option value="-1" disabled="" selected="">Elija Pieza</option>
            <option value="1">Tornillo</option>
            <option value="2">Clavo</option>
            <option value="3">Cable</option>
        </select>
    </div>
</div>

To add name to this selects, use the following JavaScript code to iterate all of them and add a custom name.

Bear in mind that you need to add a suffix to the name to avoid same name on many selects

let allSelects = $('.select-dropdown.dropdown-trigger');    
for (i = 0; i < allSelects .length; i++) {
    let item = $(allSelects[i]);
    let name= item.parent().children()[3].name;
    item.attr("name", name + '-materialize');  // change -materialize for the suffix you like  
}

Upvotes: 1

Robert
Robert

Reputation: 6967

Materialize handles <select> replacement like most other libraries; follow standard HTML conventions like name="foo" or <option value="A">But I'm showing other text here</option>.

The output will take these into consideration when creating the prettier, functional form.

Note: The Materialize CSS documentation does show the <select> tag without most of the common elements (like name=""), but I think this is more to present the cleanest, most minimal code possible.

Upvotes: 1

Related Questions