F
F
Frank_Sidebottom2018-01-29 22:17:18
JavaScript
Frank_Sidebottom, 2018-01-29 22:17:18

Location of DOM elements?

Hello. I wonder if it is possible to implement sorting of DOM elements depending on the value of the child element. Here is an example fidl. The "p" tag has a number. It is necessary that, depending on this number, the divs with the color* class move around the DOM in ascending or descending order. I wrote, for example, the number 1 in the "p" tag and this color should come first in the DOM, and so on, I think the essence is clear))

https://jsfiddle.net/kjpoycdk/2/

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2018-01-29
@Frank_Sidebottom

const $wrapper = $('.wrapper');

$wrapper
  .children()
  .sort((a, b) => $('p', a).text() - $('p', b).text())
  .appendTo($wrapper);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question