Search code examples
javascriptjqueryframeworks

Get data from Materialize CSS chips


I need to get data from Materialize CSS chips, but I don't know, how.

$('.chips-placeholder').material_chip({
    placeholder: 'Stanici přidíte stisknutím klávesy enter',
    secondaryPlaceholder: '+Přidat',
});

function Show(){
    var data = $('.chips-placeholder').material_chip('data');
    document.write(data);
}
<!-- Added external styles and scripts -->
<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.97.7/js/materialize.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.7/css/materialize.min.css">
<link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

<!-- HTML body -->

<div class="chips chips-placeholder"></div>
<button onclick="Show()" type="button">Show</button>


Solution

  • So, to access to the data's chip you just have to do this:

    var data = $('#id of your chips div').material_chip('data');
    alert(data[0].tag);`
    

    '0' is the index of your data (0, 1, 2 , 3, ...).

    'tag' is the chip content. You can also get the id of your data with '.id'.