Plummer
Plummer

Reputation: 6688

jQuery show elements with a range of values

I'm trying to get jQuery to show/hide elements based on the value of an attribute between two values.

I have markup that looks like this:

<a class="page_link" href="javascript:go_to_page(0)" longdesc="0">1</a>
<a class="page_link" href="javascript:go_to_page(1)" longdesc="1">2</a>
<a class="page_link" href="javascript:go_to_page(2)" longdesc="2">3</a>
<a class="page_link" href="javascript:go_to_page(3)" longdesc="3">4</a>
<a class="page_link active_page" href="javascript:go_to_page(4)" longdesc="4">5</a>
<a class="page_link" href="javascript:go_to_page(5)" longdesc="5">6</a>
<a class="page_link" href="javascript:go_to_page(6)" longdesc="6">7</a>
<a class="page_link" href="javascript:go_to_page(7)" longdesc="7">8</a>
<a class="page_link" href="javascript:go_to_page(8)" longdesc="8">9</a>

I have some jQuery that looks like this:

//Now, we need to paginate the pagination
var page_link_count = $('.page_link').length(); 

//Hide all the .page_links
$('.page_link').css('display','none'); 

//Show active_page
$('.active_page').css('display','block'); 

//Show up to four .page_link on either side of active_page
var active_page_value = $('.active_page').attr('longdesc'); 

$('.page_link').each(function(){

    var longdesc = $(this).attr('longdesc');

    // This next part might be what's getting me...
    if( longdesc <= active_page_value+4 && longdesc >= active_page_value-4){

        $(this).css('display','block');

    }

});

I'm trying to say, if the longdesc attribute value of any element with a page_link class is greater than the value of the sum of the active_page longdesc - 4 or less than the sum of the active_page longdesc + 4

Upvotes: 0

Views: 150

Answers (3)

bobthyasian
bobthyasian

Reputation: 943

Over complicating things...This is all you need.

//Hide all the .page_links
$('.page_link').hide(); 

//Show active_page
$('.active_page').show(); 

//Show all links before active
$('a.active_page').show();
$('a.active_page').prevAll().show();

*Note: the last two could be combined but I don't want to confuse you.

Upvotes: 0

Sushanth --
Sushanth --

Reputation: 55750

You need to parse the number before you add them . The values will be appended instead

if( (longdesc <= parseInt(active_page_value ,10) +4)
     &&( longdesc >= parseInt(active_page_value,10) -4))

JS

//Now, we need to paginate the pagination
var page_link_count = $('.page_link').length; 

//Hide all the .page_links
$('.page_link').css('display','none'); 

//Show active_page
$('.active_page').css('display','block'); 

//Show up to four .page_link on either side of active_page
var active_page_value = $('.active_page').attr('longdesc'); 

var min = parseInt(active_page_value,10) - 4;
var max = parseInt(active_page_value,10) + 4;

$('.page_link').each(function(){
    var longdesc = $(this).attr('longdesc');

    if( longdesc <= max && longdesc >= min){
        $(this).css('display','block');
    }

});​

Upvotes: 2

A. Wolff
A. Wolff

Reputation: 74420

Two things to change:

Change

var page_link_count = $('.page_link').length();

To

var page_link_count = $('.page_link').length; 

And

var active_page_value = $('.active_page').attr('longdesc');

To

var active_page_value = +$('.active_page').attr('longdesc');

Upvotes: 0

Related Questions