idan traders
idan traders

Reputation: 79

how to change function in keyCode

I would like to know that how I can change the function in the KeyCode condition? i trying to do something but I cant get inside the keyCode.

my code:

$("body").keydown(function (e) {
    // left arrow
    if ((e.keyCode || e.which) == 37) {
        prevpage('{!! !empty($data["previous"]) ? $data["previous"] : "4" !!}', '0');
    }
    // right arrow
    if ((e.keyCode || e.which) == 39) {
        nextpage('{!! $data["next"] !!}', '2');
    }
});

thanks for all the help!.

UPDATE

this its my code for the functions nextpage and prev

       function nextpage(nextpage,pagenumber) {
        getCotent(nextpage,pagenumber);
        var number = Number(nextpage)+1;
        var numberprev = Number(nextpage)-1;
        var numberpagePluss = Number(pagenumber)+1;
        var numberpageMinus = Number(pagenumber)-1;

        if(number == null || '{!! Request::segment(6) !!}' == '1' && numberpagePluss == '24' || '{!! Request::segment(6) !!}' == '2' && numberpagePluss == '27') {

            $('.next').hide();
            $('#next').hide();

        } else if(numberpagePluss == '10') {

            // $('#menu ul li:lt(5)').fadeOut('slow').hide();
            // $('#menu ul li:gt(23)').fadeIn('slow').show();
            $('#menu ul li.privie').show();
            $('#menu ul li.next').show();

        } else {

            $('.privie').attr('onclick','prevpage('+ numberprev +',' + numberpageMinus + ')');
            $('.next').attr('onclick','nextpage('+ nextpage+1 +',' + numberpagePluss + ')');

            $('#privie').attr('onclick','prevpage('+ numberprev +',' + numberpageMinus + ')');
            $('#next').attr('onclick','nextpage('+ nextpage+1 +',' + numberpagePluss + ')');

            $('.privie').show();
            $('#privie').show();


        }

    }

    function prevpage(previd,pagenumber) {

        getCotent(previd,pagenumber);
        var number = Number(previd)+1;
        var numberprev = Number(previd)-1;
        var numberpagePluss = Number(pagenumber)+1;
        var numberpageMinus = Number(pagenumber)-1;

        if(numberprev == null || numberpageMinus == '0') {

            $('.privie').hide();
            $('#privie').hide();

        } else if(numberpageMinus == '9') {

            // $('#menu ul li:lt(4)').fadeIn('slow').show();
            // $('#menu ul li:gt(23)').fadeOut('slow').hide();
            $('#menu ul li.privie').show();
            $('#menu ul li.next').show();

        } else {

            $('.privie').attr('onclick','prevpage('+ numberprev +')');
            $('.next').attr('onclick','nextpage('+ number +')');

            $('#privie').attr('onclick','prevpage('+ numberprev +')');
            $('#next').attr('onclick','nextpage('+ previd+1 +')');

            $('.next').show();
            $('#next').show();

        }

what i need its after i press left key the page its over to the next and i need the function in the keyCode will change to the next page.

Upvotes: 0

Views: 76

Answers (2)

Satish Kumar sonker
Satish Kumar sonker

Reputation: 1288

try this one

var test=function(){
    //your code
    }

    var test2=function(){
    //your code
    }

    $("document").ready(function(){
      $("body").keydown(function(e){

        if ((e.keyCode || e.which) == 37)
        {   
           test();
        }

        if ((e.keyCode || e.which) == 39)
        {
            test2();
        }   
      });
    })

Upvotes: 0

Abhishek Dhanraj Shahdeo
Abhishek Dhanraj Shahdeo

Reputation: 1356

Try this:

$("document").ready(function(){
  $("body").keydown(function(e){
    // left arrow
    if ((e.keyCode || e.which) == 37)
    {   
        console.log("condition 1");
        myfun1();
    }
    // right arrow
    if ((e.keyCode || e.which) == 39)
    {
        console.log("condition 2");
        myfun2();
    }   
  });
})

function myfun1(){
     alert("inside myfun1");
 //write your code of execution here
 }

function myfun2(){
     alert("inside myfun2");
 //write your code of execution here
 }

If it works then the keycode is running fine

Upvotes: 1

Related Questions