Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find and select all elements which have data attribute start with specific word with using jquery?

I want to find and select all element which have data attribute start with "data-qu" under wrapper div. How can I do this with using jquery?

HTML

<div class="wrapper">
    <div data-qu-lw="1"></div>
    <div data-qu-md="2"></div>
    <div data-res-kl="1"></div>
    <div data-qu-lg="3"></div>
</div>

Is there a way select those, like this $('.wrapper').find('^data-qu') or similar?

like image 970
midstack Avatar asked Dec 19 '22 00:12

midstack


2 Answers

var filteredElements = $('.wrapper > div').filter(function(){
  var attrs = this.attributes;
  for (var i=0; i<attrs.length; i++) {
      if (attrs[i].name.indexOf("data-qu")==0) return true;
  }         
  return false;
});
like image 157
Piotr Czarnecki Avatar answered Dec 24 '22 01:12

Piotr Czarnecki


ref: jQuery attribute name contains

you can do like this.

$('.wrapper div').filter(function() {
  for (var property in $(this).data()) {
    if (property.indexOf('data-qu') == 0) {
      return true;
    }
  }

  return false;
});​
like image 41
Raghavendra Avatar answered Dec 24 '22 01:12

Raghavendra