www.webdeveloper.com
Results 1 to 4 of 4

Thread: Show 5 sequential numbers of a clicked number

  1. #1
    Join Date
    Jun 2005
    Posts
    11

    Show 5 sequential numbers of a clicked number

    Hi all,

    I have a list of sequential numbers here. How can i show the 5 numbers infront and behind a clicked number?

    e.g. When i clicked on the number 0, this is the situation:

    5 4 3 2 1 0 -1 -2 -3 -4 -5

    So when i clicked on the number 2, it should show like this:

    7 6 5 4 3 2 1 0 -1 -2 -3

    Your prompt response is very much appreciated.


  2. #2
    Join Date
    Feb 2010
    Location
    Amsterdam
    Posts
    174
    you can do this:

    Code:
    function showNumber(nr){
    return [nr+5,nr+4,nr+3,nr+2,nr+1,nr,nr-1,nr-2,nr-3,nr-4,nr-5];
    }
    
    alert(showNumber(2));

  3. #3
    Join Date
    Jul 2007
    Posts
    386
    Gozzy82's example solve it nicely.

    Here is function which you can modify to increase or decrease the amount of numbers in front or behind:

    Pass num as the variable:
    Code:
    function createSequence(num)
    {
        var upper = num + 5;
        var lower = num - 5;
        var sequence = '';
        
        for (var i = upper; i >= lower; i--)
        {
            sequence += i + (i > lower ? ', ' : '');
        }
        
        alert(sequence);
    }

  4. #4
    Join Date
    Jun 2005
    Posts
    11
    Dear Gozzy82 and SparoHawk,

    Thanks for your solutions ^^

    Here is my complete code:

    Code:
    <script language="javascript">
    	jQuery(document).ready(function() {
    		listNumber(0);		
    	})
    	
    	function listNumber(val) {
    		val = parseInt(val);
    		var upper = val+5;
    		var lower = val-5;
    		var sequence = '';
    		for (var i = upper; i >= lower; i--)
    		{
    			sequence += "<span style='cursor:pointer' onclick='listNumber($(this).html());'>";
    			if (i==val) sequence += "<font weight='bold' color='#FF0000'>"  
    			sequence += i + (i > lower ? ' ' : '');
    			if (i==val) sequence += "</font>"  
    			sequence += "</span>";
    		}		
    		$('#seq_no').html(sequence);
    	}	
    </script>
    
    <div id="seq_no"></div>
    Thanks and have a nice day
    Last edited by cheany; 06-03-2010 at 04:07 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles