PDA

Click to See Complete Forum and Search --> : textbox focus


mar_zim
Sep 14th, 2004, 12:58 AM
hi to everyone im very new in this thing.
why this code wont work?
whats wrong w/ it?
what i want to achieve is when i hit the enter key it will get focus to another textbox..

<form>
<input type="textbox" name="textbox1" onkeypress="keydown(event)" >
<input type="textbox" name="textbox2">
</form>
<script>
function keydown(e){
if (!e) var e = window.event;
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
if (code==13){
textbox2.focus();
}
}
</script>


thanks in advance guys..
more power ;)

Acidic
Sep 14th, 2004, 03:16 AM
it said textbox2 is undefined. This is because you've addressed the thing wrongly, this will work:
<form>
<input type="textbox" id="textbox1" onkeypress="keydown(event)" >
<input type="textbox" id="textbox2">
</form>
<script>
function keydown(e){
if (!e) var e = window.event;
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
if (code==13){
document.getElementById('textbox2').focus();
}
}
</script>

mar_zim
Sep 14th, 2004, 04:10 AM
hi acidic i tried what you have given me but still it wont work..
what's wrong w/ it?

<form>
<input type="textbox" name="textbox1" onkeypress="keydown(event)">
<input type="textbox" name="textbox2">
</form>
<script language="javascript">
function keydown(e){
if (!e) var e = window.event;
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
if (code==13){
document.getElementById("textbox2").focus();
}
}
</script>



sorry for my stupidity im very new in this thingy.

Acidic
Sep 14th, 2004, 04:36 AM
what I posted did work. look at the function getElementByID(). You need to specify an id, not a name, for puposes on server-side code maybe you need both id and name attributes.

so change thos names back to ids and you're off.

mar_zim
Sep 14th, 2004, 08:21 PM
thanks man...


works perfect :wave: