Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select elements in sequence (by group class name)

how do I select elements created dinamicamentes by groups? I want to select msgpvtstyleme and work on them. Then select msgpvtstyle again and work on them ... Goal is to get grouped elements and insert them classes .....

enter image description here

I want to simulate chat balloons

enter image description here

result final! Thank you all!

enter image description here

like image 725
Fábio Zangirolami Avatar asked Aug 14 '16 20:08

Fábio Zangirolami


People also ask

How do you select elements with class name?

class selector selects elements with a specific class attribute. To select elements with a specific class, write a period (.) character, followed by the name of the class.

Can we get element by class name?

The getElementsByClassName() method returns a collection of elements with a specified class name(s). The getElementsByClassName() method returns an HTMLCollection. The getElementsByClassName() property is read-only.

How do you find an element with multiple classes?

Use the getElementsByClassName method to get elements by multiple class names, e.g. document. getElementsByClassName('box green') . The method returns an array-like object containing all the elements that have all of the given class names.

How do I find the first element of a class name?

If you want only the first element in the DOM with that class, you can select the first element out of the array-like HTMLCollection returned. var elements = document. getElementsByClassName('className'); var requiredElement = elements[0];


1 Answers

Here is a basic jQuery script which checks each element and detecting the following:

  • Check current class
  • If previous sibling has a different class then it will get first
  • If next sibling is of the same class, it will be middle
  • If next sibling is of another class, it will be marked as last
  • If previous and next siblings are of other classes, it will be first middle last

// define your container here
var container = $('.container'),
    currentClass = container.children(":first").attr("class");

// run through each child
container.children('li').each(function() {
  currentClass = $(this).attr("class");
  
  if ( $(this).prev().attr("class") !== currentClass ) {
  	$(this).attr("data-order","first");  }
  
  if ( $(this).next().attr("class") === currentClass && $(this).prev().attr("class") === currentClass ) {
  	$(this).attr("data-order","middle");  }
  
  if ( $(this).next().attr("class") !== currentClass ) {
  	$(this).attr("data-order","last");  }
  
  if ( $(this).next().attr("class") !== currentClass && $(this).prev().attr("class") !== currentClass ) {
  	$(this).attr("data-order","first middle last");  }
  
  // debugging only
  $(this).text( $(this).attr("class") + ': ' + $(this).attr('data-order') );
});
li[data-order~="first"] {font-weight: bold;}
li[data-order~="last"] {border-bottom:1px solid;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="container">
  <li class="class1"></li>
  <li class="class1"></li>
  <li class="class1"></li>
  <li class="class2"></li>
  <li class="class2"></li>
  <li class="class1"></li>
</ul>

jsFiddle: https://jsfiddle.net/azizn/40trqhn4/

Note: I used data-order attribute instead class as altering the class name will break the checking function (since it all revolves around class name). You can access the elements through the CSS attribute selectors [data-order="first"] for example.

like image 136
Aziz Avatar answered Oct 25 '22 22:10

Aziz