Jquery filter out the double class name with each function

aboutjquery

Here is the example code

html

<div class="class-name-1"></div>
<div class="class-name-1"></div>

<div class="class-name-2"></div>
<div class="class-name-2"></div>

...

<div class="class-name-30"></div>
<div class="class-name-30"></div>

Jquery

$('class^=class-name-').each(function() {
  console.log($(this));

  fn_myFunction();
});

This script will run sixty times fn_myFunction(), i would like to run fn_myFunction() only once for each class^=class-name-, that mean make above script run fn_myFunction() total thirty times.

Return first or last element is also ok.

And i would like to keep the jquery selector as $('class^=class-name-') if possible.

How can i do this with jquery?

Thanks.

Josh Crozier

You could build an array of unique classes and then filter the elements accordingly using the .filter() method.

In the example below, $uniqueElements is a jQuery object representing the filtered elements.

var uniqueClasses = [];
var $uniqueElements = $('[class*=class-name-]').filter(function () {
  var classes = $(this).attr('class').match(/class-name-\d+/)[0];
  return uniqueClasses.indexOf(classes) === -1 ? uniqueClasses.push(classes) : 0;
});

$uniqueElements.css('background-color', '#f00');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="first-class class-name-1 other-class">class-name-1</div>
<div class="first-class class-name-1 other-class">class-name-1</div>

<div class="class-name-2">class-name-2</div>
<div class="class-name-2">class-name-2</div>

Then you can iterate over each element in $uniqueElements and invoke your function:

var uniqueClasses = [];
var $uniqueElements = $('[class*=class-name-]').filter(function () {
  var classes = $(this).attr('class').match(/class-name-\d+/)[0];
  return uniqueClasses.indexOf(classes) === -1 ? uniqueClasses.push(classes) : 0;
});

$uniqueElements.each(fn_myFunction);

function fn_myFunction () {
  $(this).css('background-color', '#f00');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="first-class class-name-1 other-class">class-name-1</div>
<div class="first-class class-name-1 other-class">class-name-1</div>

<div class="class-name-2">class-name-2</div>
<div class="class-name-2">class-name-2</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Jquery: How to break out of each() and the function it's inside in?

From Dev

Run a jQuery function for each element with a certain class

From Dev

SAS Function to filter out dates

From Dev

jquery Scroll to class name

From Dev

How to filter out double quote symbol with replaceAll function

From Dev

jQuery Filter divs By Class

From Dev

Filter elements in a DOM with common 'class' name and apply CSS on it - JQuery

From Dev

break out of filter() function

From Dev

Function or filter vs computed attribute with css class name

From Dev

jQuery sort by class name

From Dev

filter out existing json property in $.each

From Dev

How to get the name of the class in JQuery removeClass(function)?

From Dev

jQuery count each first li element with class name

From Dev

how to use jquery to filter out children with class x

From Dev

jQuery append html element but with a different class name each time

From Dev

JQuery - Break out of $.each

From Dev

jQuery break out of $.on that is within $.each

From Dev

jQuery each() method filter with is() method

From Dev

jQuery: Run function if each tr has class

From Dev

Running a jQuery function on all divs with different values but same class name

From Dev

Use Perl to filter out the word "a" but not each letter "a"

From Dev

How to get the name of the class in JQuery removeClass(function)?

From Dev

split filter callback function out

From Dev

Javascript filter function to filter out falsy values

From Dev

Getting variable OUT of jquery.each() function

From Dev

Filter out different fields for each line with AWK

From Dev

can't able to call a function using jquery by class name

From Dev

jQuery ".each()" function only applying script to last class on page

From Dev

jQuery each in each function

Related Related

  1. 1

    Jquery: How to break out of each() and the function it's inside in?

  2. 2

    Run a jQuery function for each element with a certain class

  3. 3

    SAS Function to filter out dates

  4. 4

    jquery Scroll to class name

  5. 5

    How to filter out double quote symbol with replaceAll function

  6. 6

    jQuery Filter divs By Class

  7. 7

    Filter elements in a DOM with common 'class' name and apply CSS on it - JQuery

  8. 8

    break out of filter() function

  9. 9

    Function or filter vs computed attribute with css class name

  10. 10

    jQuery sort by class name

  11. 11

    filter out existing json property in $.each

  12. 12

    How to get the name of the class in JQuery removeClass(function)?

  13. 13

    jQuery count each first li element with class name

  14. 14

    how to use jquery to filter out children with class x

  15. 15

    jQuery append html element but with a different class name each time

  16. 16

    JQuery - Break out of $.each

  17. 17

    jQuery break out of $.on that is within $.each

  18. 18

    jQuery each() method filter with is() method

  19. 19

    jQuery: Run function if each tr has class

  20. 20

    Running a jQuery function on all divs with different values but same class name

  21. 21

    Use Perl to filter out the word "a" but not each letter "a"

  22. 22

    How to get the name of the class in JQuery removeClass(function)?

  23. 23

    split filter callback function out

  24. 24

    Javascript filter function to filter out falsy values

  25. 25

    Getting variable OUT of jquery.each() function

  26. 26

    Filter out different fields for each line with AWK

  27. 27

    can't able to call a function using jquery by class name

  28. 28

    jQuery ".each()" function only applying script to last class on page

  29. 29

    jQuery each in each function

HotTag

Archive