Solved

Dont run if first or Last

Posted on 2013-12-31
2
277 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

708 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

13 Experts available now in Live!

Get 1:1 Help Now