Answers for "jquery reorder elements"

-1

jquery reorder elements

/*
FROM:
```
<ul>
      <li>2</li>
      <li>10</li>
      <li>5</li>
      <li>7</li>
      <li>6</li>
      <li>8</li>
      <li>9</li>
      <li>4</li>
      <li>1</li>
      <li>3</li>
</ul>
```

TO:
```
<ul>
   <li>1</li>
   <li>3</li>
   <li>6</li>
   <li>7</li>
   <li>2</li>
   <li>10</li>
   <li>5</li>
   <li>8</li>
   <li>9</li>
   <li>4</li>
</ul>
```
*/

var special = $("li").filter(isSpecial).sort(byText).prependTo("ul")

function isSpecial() {
  var v = parseInt($(this).text())
  return v === 1 || v === 3 || v === 6 || v === 7
}

function byText(a, b) {
  return $(a).text().localeCompare($(b).text())
}
Posted by: Guest on May-07-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language