Calling function with object parameter from another function

Matteo NNZ

My objective is very simple: I would like to intercept right-key and left-key press and, accordingly, execute a particular JavaScript. In particular, I have two img elements in my HTML that are basically a left and a right arrow to change a central picture:

<img id="goleft" onclick="changePic(this)" src="x"/>
<img id="goright" onclick="changePic(this)" src="y" />

The function changePic(this) is hence capturing the caller object (either left or right arrow) and changing the source accordingly. However, for sampling purpose, let's say here that it will just show in an alert the id of the object:

function changePic(obj) {
    alert("hello world");
    alert(obj.id);
}

Everything works fine until here. On click event, the Javascript is properly called. However, now I'm implementing a system to capture the key-right and key-left and executing the same action. The following function will capture any key-press event:

document.onkeydown = function (evt) {
    evt = evt || window.event;
    switch (evt.keyCode) {
        case 37:
            leftArrowPressed();
            break;
        case 39:
            rightArrowPressed();
            break;
    }
};

...and, clearly, the two functions "leftArrow" and "rightArrow", I thought, could have simply done this:

function leftArrowPressed() {
    changePic(document.getElementById("goLeft"));
}

function rightArrowPressed() {
    changePic(document.getElementById("goRight"));
}

However, this does not work. The functions leftArrowPressed and rightArrowPressed are properly called (tested with an alert) but the function changePic is called (because I see the alert "hello world" popping-up but it fails on "obj.id" call). So I believe this issue is related to the way I pass the object from the caller to the listener function: could anyone please tell me what I'm doing wrong?

P.s. I'm a newbie of this language, so please don't hesitate to remark me anything that I've thought is unrelevant for answering the question but actually is not.

Musa

Ids are case sensitive, your id is goleft but you try to look it up with goLeft.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Linux

calling alloca( ) from another function call parameter?

From Dev

Python : Calling functions from inside another function parameter

From Dev

Calling a function that's in another object

From Dev

Python calling function in an function from another function

From Dev

Calling function from another Javascript file and an object is said to be null

From Dev

Calling a function within an object property from another property

From Java

Calling a function from another package

From Dev

flask calling a function from another

From Dev

Calling a function from another in ReactJs

From Dev

Calling a function from another Project

From Dev

Calling a function from another process

From Dev

Calling a function from another class?

From Dev

Calling main function from another function in C

From Dev

Calling a class function from within another function

From

Calling init function from another function

From Dev

Calling a function from another function in typescript

From Dev

Calling the BlogEntries function from another function [SilverStripe]

From Dev

Calling a Function From Another Function in PowerShell

From Dev

Python calling a function from another function in a class

From Dev

Calling php function from inside another function

From Dev

Error when calling function from another function

From Dev

Calling a function from inside another function?

From Dev

Calling variable from one function into another function

From Dev

Passing function object as a parameter to another function

From Dev

Call function from calling object

From Dev

call QML function from C++ with another QML object as parameter

From Dev

Send an object by parameter to another function when I call it from a button

From Dev

calling a function into another function

From Dev

A function calling another function

Related Related

  1. 1

    calling alloca( ) from another function call parameter?

  2. 2

    Python : Calling functions from inside another function parameter

  3. 3

    Calling a function that's in another object

  4. 4

    Python calling function in an function from another function

  5. 5

    Calling function from another Javascript file and an object is said to be null

  6. 6

    Calling a function within an object property from another property

  7. 7

    Calling a function from another package

  8. 8

    flask calling a function from another

  9. 9

    Calling a function from another in ReactJs

  10. 10

    Calling a function from another Project

  11. 11

    Calling a function from another process

  12. 12

    Calling a function from another class?

  13. 13

    Calling main function from another function in C

  14. 14

    Calling a class function from within another function

  15. 15

    Calling init function from another function

  16. 16

    Calling a function from another function in typescript

  17. 17

    Calling the BlogEntries function from another function [SilverStripe]

  18. 18

    Calling a Function From Another Function in PowerShell

  19. 19

    Python calling a function from another function in a class

  20. 20

    Calling php function from inside another function

  21. 21

    Error when calling function from another function

  22. 22

    Calling a function from inside another function?

  23. 23

    Calling variable from one function into another function

  24. 24

    Passing function object as a parameter to another function

  25. 25

    Call function from calling object

  26. 26

    call QML function from C++ with another QML object as parameter

  27. 27

    Send an object by parameter to another function when I call it from a button

  28. 28

    calling a function into another function

  29. 29

    A function calling another function

HotTag

Archive