Click to See Complete Forum and Search --> : DOCUMENT.LINKS[i].focus() : A DOUBT!!!... NEED HELP!!
ajith_rock
04-13-2007, 05:05 AM
Hi!!,
I have a code in which, on a keypress i have to check if a particular link has a focus. If it does have the focus, then the next link has to be selected. HOW DO I DO THIS?... I TRIED DOING THE FOLLOWING... BUT IN VAIN!!
function load()
{
document.links[0].focus();//ON PAGE LOAD, FOCUS SET TO FIRST LINK
}
function keyNumber(ev) {//THIS TAKES IN THE KEYPRESS CODES
nmbr=ev.which?ev.keyCode:ev.which;
switch(nmbr) {
case 38://THE UP KEY ON KEYBOARD
var i=0;
while(document.links[i].hasfocus())//DOESN WORK!!!... NEED HELP!!!!
{
i++;
}
document.links[i-1].focus();
break;
case 40://THE DOWN KEY ON KEYBOARD
var i=0;
while(document.links[i].hasfocus())//I NEED HELP WITH THIS LINE!!!
{
i++;
}
document.links[i+1].focus();
break;
}
}
I dont really know what i have to use to TEST IF THE PARTICULAR LINK HAS GOT THE FOCUS!!!.... SOMETHING WHICH RETURNS A TRUE OR A FALSE ON A LINK FOCUS WOULD DO THE TRICK I GUESS!!!
THANX A LOT FOR HELPING ME OUT!!!
WAITING IMPATIENTLY FOR A SOLUTION!!!!
CHEERS,
AJITH
gil davis
04-13-2007, 06:30 AM
WAITING IMPATIENTLY FOR A SOLUTION!!!!You will find that it does not help to yell at volunteers. Nobody here gets paid to assist those asking for help.
If you use onkeypress or onkeydown in the links, you won't have to worry about which one has focus.
ajith_rock
04-13-2007, 07:58 AM
It was just my desperation... I did not shout an all man!... I apologize incase i sounded that way!!... :o ...
Regarding the solution, Thanks for the reply first up!!... I have used onkeydown... the body section starts with this...
<BODY background="base1.jpg" BGCOLOR=#FFFFFF LEFTMARGIN=0 TOPMARGIN=0 MARGINWIDTH=0 MARGINHEIGHT=0 onkeydown="keyNumber(event)" onload="load()">
This inturn trigers the keynumber module, which has been enclosed in my first post...
I am finding a problem with the
while(document.links[i].hasfocus()), line man!.
Is there any method which returns a true for false for an onfocus of the links[i]? ...
Is there some property which stores a boolean for the onfocus of a link?
Cheers,
Ajith
P.S. I APOLOGIZE ONCE AGAIN...
gil davis
04-13-2007, 08:43 AM
<BODY background="base1.jpg" BGCOLOR=#FFFFFF LEFTMARGIN=0 TOPMARGIN=0 MARGINWIDTH=0 MARGINHEIGHT=0 onkeydown="keyNumber(event)" onload="load()">If you use onkeydown on each link you wouldn't need to tell what the focus is.
while(document.links[i].hasfocus()), line man!.hasFcous() only applies to the document.
Is there any method which returns a true for false for an onfocus of the links[i]? ...
Is there some property which stores a boolean for the onfocus of a link?
No, but you can make one. You would have to add an onfocus event to each link to update the property. If you have to add an event to each link, you may as well add the onkeydown instead and be done with it.
You can run a script onload that assigns the handler to each link in the document.links array, or document.getElementsByTagName("A") array.
toicontien
04-13-2007, 08:54 AM
I don't think there's a browser function to detect if something has focus, but you could hack it.
window.onload = function() {
initializeLinkFocusDection();
};
function initializeLinkFocusDection() {
if (document.getElementsByTagName) {
var links = document.getElementsByTagName('a');
for (var i=0, end=links.length; i<end; i++) {
links[i].rel = 'noFocus';
links[i].onfocus = function() {
this.rel = 'hasFocus';
};
links[i].onblur = function () {
this.rel = 'noFocus';
};
links[i] = null;
}
}
}
function isFocused(el) {
if (document.getElementById) {
if (typeof(el) === 'string') {
el = document.getElementById(el);
}
return el.rel === 'hasFocus';
}
}
The rel attribute is a standard attribute and means "relation." These functions give each <a> tag a rel="noFocus" when the page finishes loading. When the link is in focus, the rel attribute is changed to "hasFocus". You can simply call the isFocused() function to determine if an element has focus according to the rel attribute. You can pass the isFocused() function the Id of the link to check, or a node reference to the link.
ajith_rock
04-13-2007, 02:31 PM
hey davis and toicontien!,
Thanks a ton for the advice and the code tips!.. It worked wonderfully well!... :) .. I used a select case and finished the job... Just sharing my solution with you.. A BIGG HUG FOR HELPIN ME OUT!!!... :)
THE FUNCTION ::
function keyNumber(ev,day) {
nmbr=ev.which?ev.keyCode:ev.which;
switch(nmbr) {
case 37:
document.links[0].focus();
break;
case 38:
switch(day)
{
case tuesday:
{
document.links[0].focus();
break;
}
case wednesday:
{
document.links[1].focus();
break;
}
case thursday:
{
document.links[2].focus();
break;
}
case friday:
{
document.links[3].focus();
break;
}
case bac:
{
document.links[4].focus();
break;
}
case buy2:
{
document.links[6].focus();
break;
}
case buy3:
{
document.links[7].focus();
break;
}
case buy4:
{
document.links[8].focus();
break;
}
default:
ALERT("USE ONLY LEFT,RIGHT,UP,DOWN AND RETURN");
break;
}
break;
case 39:
document.links[6].focus();
break;
case 40:
switch(day)
{
case monday:
{
document.links[1].focus();
break;
}
case tuesday:
{
document.links[2].focus();
break;
}
case wednesday:
{
document.links[3].focus();
break;
}
case thursday:
{
document.links[4].focus();
break;
}
case friday:
{
document.links[5].focus();
break;
}
case buy1:
{
document.links[7].focus();
break;
}
case buy2:
{
document.links[8].focus();
break;
}
case buy3:
{
document.links[9].focus();
break;
}
case buy4:
{
document.links[10].focus();
break;
}
default:
document.links[7].focus();
break;
}
break;
}
}
HOW I CALL THE FUNCTION (JUST A SAMPLE)::
<a href="monday.htm" id="monday" onkeydown="keyNumber(event,monday)"><img src="monday.jpg" BORDER=0 id="monday"></img></a><BR>
------------------------------------------------------------
The code works awesome!... But it requires the "id" to be specified in the <A HREF> tag!!... Just out of interest, i am asking... Why does it happen?... :) ...
THANX A LOT FOR HELPIN ME OUT GUYS!!
YOU ROCK!!!!
CHEERS,
AJITH
P.S. Sorry once again for sounding harsh the first time davis.. AN THANX A TON FOR THE CODE TOICONTIEN!!.... :D