Selecting Bootstrap Attributes

1

Well, I'm having trouble selecting an attribute of a certain element of bootstrap with jquery , for example:

<ol class="carousel-indicators">
    <li data-target="#carrossel-principal" data-slide-to="0" class="active"></li>
    <li data-target="#carrossel-principal" data-slide-to="1"></li>
    <li data-target="#carrossel-principal" data-slide-to="2"></li>
</ol>
  • To select data-target , or data-slide-to , and their respective values, how could this be done?
  • Is the same procedure valid for any other attribute?
  • How can I get the value of only data-slide-to that has class active ?
asked by anonymous 16.02.2017 / 11:44

2 answers

3

See if it works:

$('.carousel-indicators li').data('slide-to');
$('.carousel-indicators').data('target');

Putting data the way you put it in HTML works.

    
16.02.2017 / 11:53
2

DATA ATTRIBUTES

Add Attributes with Data (Data Attributes) targets the extensibility of tags in HTML5. Attributes can be accessed in two ways, via pure JavaScript or jQuery.

With pure JavaScript via dataset ( SOURCE ):

p>

function mostrar() {
alert(document.getElementById('elemento').dataset.codigo);
}

/* Repare no uso do DATASET pois é ele que te permite acessar qualquer valor do tipo data-??? */
<p data-codigo="18" onclick="mostrar()" id="elemento">Clique aqui!</div>

And with jQuery via .data() ( SOURCE ):

$('div').click(function() {
  alert($('div').data('qualquer'));
});

/* a função .data() permite capturar atributos data- */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divdata-qualquer="Trabalhando com data-">Clique Aqui!</div>
    
16.02.2017 / 11:58