1

I have the following code, its supposed to reorder my divs within #list. And it does that just fine. Now what I want to add is: if a div does not have the attribute "data-l-disc" don't add it/hide it.

<select id="selectid">
  <option id="valid1" value="def">Default order</option>
  <option id="valid2" value="hl">h-l</option>
</select>
<div id="list">
---------the follow div is used about 50*-------------
 <div class="dbl" data-l-disc="1">
 ---------blabla (like 10 other divs are in here)----------
 </div>


</div>
jQuery(document).ready(function( $ ) {
  $('select').on('change', function() {
    if(document.getElementById('selectid').value == "hl") {
      var dList = $(".dbl");
      dList.sort(function(a, b){ return $(b).data("l-disc")-$(a).data("l-disc")});
      $("#list").html(dList);
    }
  })
});
3
  • What do you mean by have a "tag"? Do you mean attribute? <div> is a tag; if you had the tag <div data-l-disc="something">, the data-l-disc="something" part is an attribute. Commented May 1, 2017 at 13:22
  • thnx, i ment attribute then ;) Commented May 1, 2017 at 13:23
  • An example of the structure of #list would help in answering your question. Commented May 1, 2017 at 13:24

1 Answer 1

3

you can hide it by using

$(".dbl").not("[data-l-disc]").hide()
Sign up to request clarification or add additional context in comments.

3 Comments

awesome that worked, now i got a follow up question.the complete code has like 20 attributes/20 different filters. now if i run this the div stays hidden even if i select another option. is there a simple way i can say (each time the code is run) select all 'attributes' again, then re-filter with that specific code?
it's better to ask this question as another question
solved that issue by using your code but with .filter and.show, all my divs had 1 attribute in commen so used that

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.