Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

focus text 1

Status
Not open for further replies.

dijo123

Programmer
Jan 13, 2004
30
SE
Hello,
I wonder how to focus a text within the <a> </a> tag with means of JavaScript. (The tabulation shall start from that text.)
Like:
Code:
var el=document.getElementById("menu1");
el.getfocus();

/Diana
 
you're close:

var el=document.getElementById("menu1");
el.focus();


=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
My code look like this:
Code:
<div id="menu0" class="clF">&nbsp;
  <a href="template_07.htm" class="clS">Accessories</a><br>
  <a href="template_07.htm" class="clS">Remove</a><br>
</div>
It worked if I gave the <a> tag a id. Is there a possibility to give focus to the div tag and then step down to the a tag. Like:
Code:
var el=document.getElementById("menu0");
	el.focus()+1;
 
something like this?

Code:
<script type="text/javascript">
function focusNextElement(el) {
	var cNodes = el.childNodes;

	for (var x = 0; x < cNodes.length; x++) {
		if (cNodes[x].nodeType == 1) {
			cNodes[x].focus();
			break;
		}
	}
}
</script>

<div id="menu0" class="clF">&nbsp;
  <a href="template_07.htm" class="clS">Accessories</a><br>
  <a href="template_07.htm" class="clS">Remove</a><br>
</div>

<input type="button"
  value="focusNextElement"
  onclick="focusNextElement(document.getElementById('menu0'))" />

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Yes, that is what I wanted!

Do you also knew how to get the id of the focused element right now?
I want to get the id of the focused element and save it into a cookie(in onUnload). Next time the page is opened, the element is focused again.


/Diana
 
this should do it:
Code:
function focusNextElement(el) {
    var cNodes = el.childNodes;

    for (var x = 0; x < cNodes.length; x++) {
        if (cNodes[x].nodeType == 1) {
            cNodes[x].focus();
           [b] var id = cNodes[x].getAttribute("id");[/b]
            break;
        }
    }
}

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
realize though that in the above code, var "id" will only exist within the "if" block...perhaps this is better:
Code:
function focusNextElement(el) {
    var id;
    var cNodes = el.childNodes;

    for (var x = 0; x < cNodes.length; x++) {
        if (cNodes[x].nodeType == 1) {
            cNodes[x].focus();
            id = cNodes[x].getAttribute("id");
            break;
        }
    }

    //  now do something with "id"
}

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
It worked if I wrote:

Code:
if (cNodes[x].nodeType == 1 && cNodes[x].focus) {
    cNodes[x].focus();
    break; 
}

It seems that I have elements that not can be set to focus.
Anyway, the getAttribute did not work. It says getAttribute is not a function. My brower may not support that, I don't know.
But how shall I know if a node is set to focus or not?
Like:
Code:
for (var x = 0; x < cNodes.length; x++) {
     if (cNodes[x].isFocused) {
        break; 
     }
}

Thank you very much for helping me!


/Di
 
>> "It says getAttribute is not a function"

it's probably your browser - this is the proper way to do it. works fine in Mozilla Firefox and IE6

>> "But how shall I know if a node is set to focus or not?"

there is no cross-browser method of determining what element has focus. you'll have to handle this yourself, perhaps attach script to the "onfocus" handler for the elements you want to watch. this script could set a global variable to store the element (or reference to it) that triggered it

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
ok, so there is no keyPress, or "someone-focus-something" or onClick function like in Java programming that waits for a special keypress or click event anywhere on the site? The functions must be attached to an element?

/Diana
 
srcElement is IE only.

you can listen for keypress or clicks anywhere, like

document.body.onclick = someFunction;

but you'll only be notified when that specific element receives the event. there's no generic onclick or onfocus handler to notify when *any* element receives that event. you could use script though to attach a function to every element for you.


=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top