Solved

Dont run if first or Last

Posted on 2013-12-31
2
278 Views
Last Modified: 2013-12-31
Im trying to write some code  which when up or down keys are pressed the the previous or next is selected, however if the first element is selected then not to do anything on the up key, and if the last is selected dont do anything on down key.

So what I tried to do is put in an if statment:-
if ($(".selected") != $(".selection").last()) {

Open in new window


However it still doesnt stop.

Ive uploaded my code to JSFiddle.net in the hope an expert can show me where Im going wrong.

The up and down keys should only work when the textbox is selected.
0
Comment
Question by:tonelm54
2 Comments
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39748290
You could throw a class "first" and "last" and test for that.  Or would you rather loop through each div perhaps $('.selection')each(function(){
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39748291
Use .prev(".selection").length and .next(".selection).length

Test page : http://jsfiddle.net/YRLpx/1/

$(document).ready(function () {
    $("#testBox").keydown(function(event) {
        if (event.which == 40) {
            $("#testConsole").html("Down Pressed");
            $selElement = $(".selected");
            if ($selElement.next(".selection").length) {
                $(".selected").next(".selection").addClass("selected");
                $selElement.removeClass("selected");
                }
            }
        if (event.which == 38) {
            $("#testConsole").html("Up Pressed");
            $selElement = $(".selected");
            if ($selElement.prev(".selection").length) {
                $(".selected").prev(".selection").addClass("selected");
                $selElement.removeClass("selected");                 
                }           
            }        
        if ( event.which == 13 ) {
           event.preventDefault();
          }        
        
        //else {
        //    $("#testConsole").html(event.which);
        //}

        });    
    });
    

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now