Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dont run if first or Last

Posted on 2013-12-31
2
Medium Priority
?
286 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
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…

670 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