Solved

Dont run if first or Last

Posted on 2013-12-31
2
285 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 53

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

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

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

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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…

617 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