Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting Elements Based on CSS Class and Text not working

I am trying to reorganize some information that is created by server-side code. The server is strapped for memory, and all sorting and displaying will need to be handled client-side with javascript/jquery. The html is along the lines of...

<div>
    <a href="https://en.wikipedia.org" class="AccessSitesLinks true 1">Wikipedia Home</a>
    <a href="https://en.wikipedia.org/wiki/Gold" class="AccessSitesLinks false 1">Wikipedia Gold</a>
    <a href="https://google.com" class="AccessSitesLinks true 2">Google Home</a>
    <a href="https://mail.google.com/" class="AccessSitesLinks false 2">Google Mail</a>
    <a href="https://en.wikipedia.org/wiki/Mushroom" class="AccessSitesLinks false 1">Wikipedia Mushrooms</a>
    <a href="https://facebook.com" class="AccessSitesLinks true 3">FaceBook Home</a>
    <a href="https://facebook.org/about" class="AccessSitesLinks false 3">FaceBook About</a>
</div>

Here is my fiddle in progress https://jsfiddle.net/ydc6ywuz/1/ The overall goal is to sort AccessSitesLinks true to be the root sites. Meaning any css class that is false should be appended to the root site based on the number after false. The best example is Wikipedia Home is true and 1, sites like mushrooms and gold would be false and 1.

This is not where my issue is. When I run this Javascript code. The sort works perfectly. but the href values remain the same. Despite them being correct in the Console.log portion.

    function setFields() {
    var sortSite = $('.AccessSitesLinks.true');
    var arr = sortSite.map(function(_, o) {
        return {
          t: $(o).text(),
          h: $(o).attr('href'),
          c: $(o).attr('class')
        };
      }).get();
      arr.sort(function(o1, o2) {
            return o1.t > o2.t ? 1 : o1.t <o2.t ? -1: 0;
      });

      sortSite.each(function(i, o) {
        console.log(i);
        $(o).text(arr[i].t);
        $(o).attr(arr[i].h);
        $(o).attr(arr[i].c);
        console.log(arr[i].h);
        console.log(arr[i].c);
      });

Edit: I tried doing $(o).attr('href') = arr[i].h; but this did not work Uncaught ReferenceError: invalid lef-hand side in assignment

like image 643
christopher clark Avatar asked Dec 31 '15 15:12

christopher clark


People also ask

Why CSS properties are not working?

Make sure the link tag is at the right place If you put the <link> tag inside another valid header tag like <title> or <script> tag, then the CSS won't work. The external style CAN be put inside the <body> tag, although it's recommended to put it in the <head> tag to load the style before the page content.

Can we use text in CSS selector?

The inner texts are the string patterns that the HTML tag manifests on the web page. ':' is used to symbolize contains method. Click on the "Find target in page" button to check whether the defined CSS Selector locates the desired element or not.

How do you override a class in CSS?

To override the CSS properties of a class using another class, we can use the ! important directive. In CSS, ! important means “this is important”, and the property:value pair that has this directive is always applied even if the other element has higher specificity.

What CSS selector should we use if we want to select all elements but not the specified class or selector?

In CSS, to exclude a particular class, we can use the pseudo-class :not selector also known as negation pseudo-class or not selector. This selector is used to set the style to every element that is not the specified by given selector.


1 Answers

These lines are the problem:

    $(o).attr(arr[i].h);
    $(o).attr(arr[i].c);

You need to provide the attribute names:

    $(o).attr("href", arr[i].h);
    $(o).attr("class", arr[i].c);
like image 108
Pointy Avatar answered Sep 18 '22 12:09

Pointy