1

How would you find using jQuery an element with maximal "page" value which is less than "6" ?

Suppose that "page" has only numerical values, and that the elements are sorted by "page". For example:

<div id="wrapper">
  <div page="1"></div>
  <div page="2"></div>
  <div page="4"></div>      => answer
  <div page="6"></div>
  <div page="7"></div>
  <div page="9"></div>
</div>
4
  • Does it have to be done with jQuery? Commented Jun 5, 2011 at 10:12
  • there's no built-in jQuery support for doing this, it just makes iterating over the elements and accessing their attributes much easier than pure JS. Commented Jun 5, 2011 at 10:18
  • Question: Does always an element exist the value you are looking for (i.e. 6)? Are the elements always sorted in the DOM tree (i.e. sorted by page value)? Commented Jun 5, 2011 at 10:20
  • I can't assume that there is an element with value 6. But, I can assume that the elements are sorted by "page" (I edited the question). Commented Jun 5, 2011 at 10:25

4 Answers 4

2

You can use filter to select all the selectors less than 6, and then select the last one.

$('#wrapper div').filter(function() {
  return $(this).attr('page') < 6;
}).last().css('color', 'red');

example: http://jsfiddle.net/niklasvh/rFYfM/

Sign up to request clarification or add additional context in comments.

2 Comments

I just finished writing exactly the same code (but without css)
@Felix Kling Good point. I presumed from the question that they were
2

Since you didn't (originally) specify whether the list is sorted, I'll assume they're not, in which case you must iterate over all of the elements and record the one that matches:

var maxEl = null;
var maxNum = 0;

$('#wrapper > div').each(function() {
    var num = $(this).attr('page');
    if (num > maxNum && num < 6) {
        maxNum = num;
        maxEl = this;
    }
});

// result is in maxEl

See http://jsfiddle.net/alnitak/WdT2m/ for working demo

1 Comment

Thanks for your general answer, but Niklas has guessed better my intention via the example.
0

This works for unsorted list:

i = 0;
$('div').filter(function(){
    $thispage = $(this).attr('page')
    if($thispage < 6 && $thispage > i){
        i = $thispage;
        return true;
    }
    return false;
}).last()

Comments

0
$("#wrapper > div[page]")
.filter( function() { return parseInt($(this).attr("page")) < 6; } )
.sort( function(a,b) { return parseInt($(a).attr("page")) - parseInt($(b).attr("page")); } )
.last();

Broken down:

  • Grabs all div's immediately within your wrapper that have a page attribute.

  • Filters to include only those elements that are less than 6.

  • Sorts these elements. (can skip this if you know your div's are sorted).

  • Selects the last element found, and thus the element that meets your condition.

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.