Why focus moves to top of document when removing element?

H

Hvid Hat

Hi

I've got a list of paragraphs that when clicked are deleted from the DOM
with JavaScript. When I remove an element with removeChild the focus moves
to the top of the document. Why is that and how do I prevent it from happening.

I've set up this test page : http://hvidhat.users.whitehat.dk/ecmascript.html

If you click on paragraph 30 at the bottom, the paragraph is deleted and
focus moves to the top of the document. How can I prevent the focus moving
to the top? I want the focus to stay where the paragraph was deleted. I hope
it makes sense :-
 
E

Evertjan.

Hvid Hat wrote on 23 jan 2008 in comp.lang.javascript:
I've got a list of paragraphs that when clicked are deleted from the
DOM with JavaScript. When I remove an element with removeChild the
focus moves to the top of the document. Why is that and how do I
prevent it from happening.

I've set up this test page :
http://hvidhat.users.whitehat.dk/ecmascript.html

If you click on paragraph 30 at the bottom, the paragraph is deleted
and focus moves to the top of the document. How can I prevent the
focus moving to the top? I want the focus to stay where the paragraph
was deleted. I hope it makes sense :)

No, it does not.

How can the focus stay on somthing that does not exist anymore?

You will specifically have to tell the browser where you want the focus
to go.

.... and should be glad the browser builders chose the top element as a
default.
 
S

stannyc

Hi

I've got a list of paragraphs that when clicked are deleted from the DOM
with JavaScript. When I remove an element with removeChild the focus moves
to the top of the document. Why is that and how do I prevent it from happening.

I've set up this test page :http://hvidhat.users.whitehat.dk/ecmascript.html

If you click on paragraph 30 at the bottom, the paragraph is deleted and
focus moves to the top of the document. How can I prevent the focus moving
to the top? I want the focus to stay where the paragraph was deleted. I hope
it makes sense :)

Use the .previousSibling property to change the focus to the
immediately preceding item, and THEN delete the clicked item.
 
T

Thomas 'PointedEars' Lahn

Hvid said:
I've got a list of paragraphs that when clicked are deleted from the DOM
with JavaScript. When I remove an element with removeChild the focus moves
to the top of the document. Why is that
href="#"

and how do I prevent it from happening.

onclick="...; return false;"

Besides, you don't need the links for this feature, and you really want to
employ event bubbling. http://www.w3.org/TR/DOM-Level-2-Events/


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top