0

i use a javascript routine with jquery3.6.1 to order divs as follow when the button #tri is clicked:

        $("#tri").click(function() {
        var $divs = $("#resultats div.notice");
        var alphabeticallyOrderedDivs = $divs.sort(function (a, b) {
            return $(a).find("div.bibloc:first").text() > $(b).find("div.bibloc:first").text();
        });
        $("#resultats").html(alphabeticallyOrderedDivs);
    
    });

the divs are constructed like this (inside the #resultat div):

    <div class='notice' class='2022'>
        <div class='internotice'></div>
        <div class='notice_header'>
            <p class='title'><img src='img1.png' /></p>
            <h4>Title1</h4>
            <p>details</br>info</p>
        </div>
        <div class='bibloc'>
            <p class='enrayon'> ref1<span>text</span></p>
        </div>
    </div>
    
    <div class='notice' class='2022'>
        <div class='internotice'></div>
        <div class='notice_header'>
            <p class='title'><img src='img1.png' /></p>
            <h4>Title2</h4>
            <p>details</br>info</p>
        </div>
        <div class='bibloc'>
            <p class='enrayon'> ref2<span>text</span></p>
        </div>
    </div>

it works perfectly in firefox, all the divs are sorted with "ref1,ref2..."

no other browser i tested want to do the sort. no error message in console, just doesn't work

any idea of what could be the source of this problem ? thanks

tried everything, change or simplify html, move javascript function but nothing work

2
  • 1
    Possibly this sort: return ($(a).find("div.bibloc:first").text()) > ($(b).find("div.bibloc:first").text()) ? 1 : -1; Commented Dec 7, 2022 at 0:47
  • 1
    Please provide enough code so others can better understand or reproduce the problem. Commented Dec 7, 2022 at 9:42

2 Answers 2

1

For sort, be specific, and return 1 or -1:

return ($(a).find("div.bibloc:first").text()) > ($(b).find("div.bibloc:first").text()) ? 1 : -1;
Sign up to request clarification or add additional context in comments.

Comments

0

thanks Peter Thoeny !!!!

the solution was effectively :

return ($(a).find("div.bibloc:first").text()) > ($(b).find("div.bibloc:first").text()) ? 1 : -1;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.