/    Sign up×
Community /Pin to ProfileBookmark

keyCode’s equivalent in firefox?

I am building a page in asp.net c#. In it I have a textbox and a dropdownlist. On typing in textbox, if the value is found in dropdownlist, it should be selected. Also on pressing the up and down keys in the textbox the dropdownlist gets the focus. On pressing enter in dropdownlist the textbox is filled with dropdownlist’s value. Here is the javascript code I have used, it is working perfectly in IE but not in firefox.

I used “alert” to detect where the problem is. In firefox the code stops working after the bolded part. I’ve done a little research and found that keyCode only works for IE. So what whould I write to get the keycode in firefox?

<script language=javascript>

var UPKEY = false;

var DOWNKEY = false;

var ENTERKEY = false;

function KeyDownHandler(e)

{
UPKEY = false;

DOWNKEY = false;

ENTERKEY = false;

var x = ”;

if (document.all)

{

var evnt = window.event;

x = evnt.keyCode;

}

[B]else

{

x = e.keyCode;

}[/B]

DetectKeys(x, true);

showValue();

}

function KeyDownHandler2(e)

{
UPKEY = false;

DOWNKEY = false;

ENTERKEY = false;

var x = ”;

if (document.all)

{

var evnt = window.event;

x = evnt.keyCode;

}

else

{

x = e.keyCode;

}

DetectKeys(x, true);

enterValue();

}

function DetectKeys(KeyCode, IsKeyDown)

{

if (KeyCode == ’38’)

{

UPKEY = IsKeyDown;

}

else if (KeyCode == ’40’)

{

DOWNKEY = IsKeyDown;

}

else if (KeyCode == ’13’)

{

ENTERKEY = IsKeyDown;

}

}

function showValue()
{

if(UPKEY)
{

document.getElementById(“ctl00_PageContent_LBStations”).focus();
}
else if(DOWNKEY)
{

document.getElementById(“ctl00_PageContent_LBStations”).focus();
}
else
{

txt = document.getElementById(“ctl00_PageContent_txtNewStation”).value;
count = document.getElementById(“ctl00_PageContent_LBStations”).options.length;

for (i = 0; i < count; i++)
{

var drp = document.getElementById(“ctl00_PageContent_LBStations”).options[i].text;
len = txt.length
sub = drp.substring(0, txt.length)
if (sub == txt)
{
document.getElementById(“ctl00_PageContent_LBStations”).selectedIndex = i
}
}
}
}

function enterValue()
{

if(ENTERKEY)
{
var ddlStation = document.getElementById(“ctl00_PageContent_LBStations”);
var Text = ddlStation.options[ddlStation.selectedIndex].text;
var Value = ddlStation.options[ddlStation.selectedIndex].value;
document.getElementById(“ctl00_PageContent_txtNewStation”).value = Text;
document.getElementById(“ctl00_PageContent_txtHiddenStation”).value = Value;
document.getElementById(“ctl00_PageContent_txtNewStation”).focus();
}

}

</script>

to post a comment
JavaScript

1 Comments(s)

×

Success!

Help @drkilljoy 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 4.28,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...