/    Sign up×
Community /Pin to ProfileBookmark

Help with defining variable and assigning new value

I have an ajax callback function that is supposed perform 2 tasks; the second task is what I need help with.

It is supposed to bind an anonymous function to the onkeypress event. This anonymous function is supposed to decrease or increase the value of a variable “p” based on if the up or down arrow key is pressed. The variable “p” is originally set to null and if the up key is pressed the value of p should be decreased by 1 and conversely if the down key is pressed the value of p should be increased by 1.

Currently, however, if the up key is pressed, regardless of how many times, the value of “p” is always 1 and if the down key is pressed, regardless of how many times, the value of “p” is always 0. Below is my code, can someone suggest/tell me where the problem in my code is? Thanks.

[code]
function suggestionCallback(ajaxResponse){
if (!ajaxResponse.match(/Error/i) && ajaxResponse != “”){
$(“#suggestionBox”).show();
var suggestionList = document.getElementById(“suggestionList”);
suggestionList.innerHTML = ajaxResponse;

var inputBox = document.getElementById(“msgRecipient”);
var li = suggestionList.getElementsByTagName(“li”);
var p = null;

inputBox.onkeypress = function(e){

// If [TAB] or [ENTER] keys are pressed
if ( e.keyCode == 9 || e.keyCode == 13 ){

} else if ( e.keyCode == 38 ){ // If the up key is pressed, select the previouse user or the last user if at the beginning
p = (p == null) ? li.length-1 : p-1;
if (p < 0) p = li.length-1;
alert(“p: ” + p);
updatePos( li[p] );
} else if ( e.keyCode == 40 ){ // if the down key is pressed, select the next user or the first user if at the end
p = (p == null) ? 0 : p+1;
if (p > li.length-1) p = 0;
alert(“p: ” + p);
updatePos( li[p] );
}

}; // end binding of function to input.onkeypress

}
}
[/code]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@toicontienJan 06.2009 — Just some incorrect mathematical logic is all:
[CODE] } else if ( e.keyCode == 38 ){ // If the up key is pressed, select the previouse user or the last user if at the beginning
[B]if (p === null || p == 0) {
p = li.length - 1;
}
p--;[/B]
updatePos( li[p] );
} else if ( e.keyCode == 40 ){ // if the down key is pressed, select the next user or the first user if at the end
[B]if (p === null || p == li.length) {
p = 0;
}
p++;[/B]
updatePos( li[p] );
}[/CODE]
Copy linkTweet thisAlerts:
@Dan0authorJan 06.2009 — Thanks toicontien, I tried:

if (p === null || p == 0) {

p = li.length - 1;

}

p--;
[/quote]


and

if (p === null || p == li.length) {

p = 0;

}

p++;
[/quote]


But the same root problem persists, where the value of p is always 0 or 1. With the above changes, when the up key is pressed the value is always 0 and when the down key is pressed the value is always 1 - this is the reverse from my original code - but the same root problem.

The problem seems to be that every time the arrow keys are pressed the value of "p" always starts at null, the variable "p" never takes on and stores the new value after one of the arrow keys has been pressed. Anyone know why?
Copy linkTweet thisAlerts:
@toicontienJan 06.2009 — ah, ok! The [B]p[/B] variable needs to be a global variable then.
[CODE][B]var p = null;[/B]
function suggestionCallback(ajaxResponse){
if (!ajaxResponse.match(/Error/i) && ajaxResponse != ""){
$("#suggestionBox").show();
var suggestionList = document.getElementById("suggestionList");
suggestionList.innerHTML = ajaxResponse;

var inputBox = document.getElementById("msgRecipient");
var li = suggestionList.getElementsByTagName("li");
[B]// remove this line -> var p = null;[/B]
[/CODE]
Copy linkTweet thisAlerts:
@Dan0authorJan 06.2009 — The scope of variable p was the problem, once I removed it from the callback function and declared/defined it outside of the function the code works fine.

But I don't understand why it didn't work in the first place - based on what I understand of scope - doesn't a nested function have access to the enclosing function's local variables? If anyone could clarify the scope rules it would be appreciated. Thanks.
Copy linkTweet thisAlerts:
@toicontienJan 06.2009 — Variables local to a function are created when the function begins executing, and are destroyed when the function ends execution. The [B]p[/B] variable, when defined local to a function, is repeated and destroyed each time the function is executed, and the value is not carried over to the next execution.

Each time the suggestionCallback function was executed, the [B]p[/B] variable was set back to null.
Copy linkTweet thisAlerts:
@Dan0authorJan 06.2009 — Thanks toicontien. I appreciate the coding help and the clarification.
×

Success!

Help @Dan0 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...