最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Auto Sorting List Alphabetically

programmeradmin0浏览0评论

I'm trying to simply sort unordered lists when they have the following ID.

The code works in a fiddle perfectly /

but when i try to run it on my site it doesn't work or give me any errors. any ideas?

function sortUL(selector) {
var unsortedList = jQuery(selector);
unsortedList.find('li').sort(function (a, b) {
    var upA = jQuery(a).text().toUpperCase();
    var upB = jQuery(b).text().toUpperCase();
    return (upA < upB) ? -1 : (upA > upB) ? 1 : 0;
}).appendTo(selector);
};

jQuery(document).ready(function () {
    sortUL("#sortList");
});

I'm trying to simply sort unordered lists when they have the following ID.

The code works in a fiddle perfectly https://jsfiddle/w19Lbjqt/2/

but when i try to run it on my site it doesn't work or give me any errors. any ideas?

function sortUL(selector) {
var unsortedList = jQuery(selector);
unsortedList.find('li').sort(function (a, b) {
    var upA = jQuery(a).text().toUpperCase();
    var upB = jQuery(b).text().toUpperCase();
    return (upA < upB) ? -1 : (upA > upB) ? 1 : 0;
}).appendTo(selector);
};

jQuery(document).ready(function () {
    sortUL("#sortList");
});

Share Improve this question edited May 8, 2019 at 21:33 Espi asked May 8, 2019 at 16:21 EspiEspi 215 bronze badges 4
  • How are you trying to run it on your site? Where are you enqueueing it? – WebElaine Commented May 8, 2019 at 16:41
  • 3 Open the browser dev tools (press F12 on Chrome or FF), go to "Console" and paste your code. If things work as you want, then your code is not being loaded and you should investigate why. You can also use the dev tools for this (check if your JS file is being loaded on the "network" tab). – Daniel Loureiro Commented May 8, 2019 at 17:33
  • I verified my scripts file is for sure being loaded. Its being enqueued from my functions file. Any other ideas? – Espi Commented May 8, 2019 at 18:57
  • when trying to add the code to the console directly it does nothing also. no errors. nothing. Page in question (employee.provo.edu/human-resource/job-descriptions/…) – Espi Commented May 8, 2019 at 19:29
Add a comment  | 

1 Answer 1

Reset to default 0

I was able to fix this by adjust my call slightly. instead of using

jQuery(document).ready(function () {
    sortUL("#sortList");
});

I used

jQuery(window).on("load", function() {
     sortUL("#sortList");
});

seems that either SHOULD have worked, but this worked for me nonetheless

发布评论

评论列表(0)

  1. 暂无评论