Jump to content

setting focus on an element


Elemental

Recommended Posts

Hey Folks, I have an external javascript that setfocus() on an input field in a form on window.onload, works great everywhere thanks to the Masters on this forum, but here's my question...Let's say I have this input field sitting further down on the page, where it can't be seen when the page loads, how can I still set the focus on that input field without having the page jump to it on the onload call?Current JavaScript Code:

function setFocus(){document.getElementById("name").focus();}window.onload = function (){document.getElementById("name").value="";document.getElementById("email").value="";setFocus();}

Peace,Elemental

Link to comment
Share on other sites

I don't think you can.
I agree, I don't think it's possible to set focus on a hidden or non-displayed element. If I recall correctly, you also can't read from it or set its properties. Or do anything to it, except verify its present.
Link to comment
Share on other sites

I agree, I don't think it's possible to set focus on a hidden or non-displayed element. If I recall correctly, you also can't read from it or set its properties. Or do anything to it, except verify its present.
I think he meant that it would be out of the page's initial view.Since i too don't believe you can prevent the page from jumping down, you could instead change one of the CSS properties of the input filed to make it stand out. i.e. background-color, font color, etc.
Link to comment
Share on other sites

I think he meant that it would be out of the page's initial view.
You're right, I didn't read the post carefully enough. I'll just plead insanity.
Link to comment
Share on other sites

You're right, I didn't read the post carefully enough. I'll just plead insanity.
wirehopper, End User, thescientist, Thank you All for your replies and comments, it's appreciated.Okay, so since the setfocus() event will force the element to come into view, is there a way to set the focus on the field when the user scrolls down the page and the field is then visible within the window?I know one can test for the "Window Size" (not all browsers support this or so I've read) but given this possibility could it be possible then, having this information, to fire such a code when the element you want to set the focus on comes into "Window" view?Just curious.Peace,Elemental
Link to comment
Share on other sites

Okay, so since the setfocus() event will force the element to come into view, is there a way to set the focus on the field when the user scrolls down the page and the field is then visible within the window?
Hmmm...I know you can detect the percentage of page scrolled and also detect when you've hit the bottom of the page, but I don't know if there's a way to directly detect an element coming into view. I think it could be done, though...you could probably test the top/bottom positions of the element in question against the the scrolltop property and the height property of the containing area.
Link to comment
Share on other sites

or just "highlight" the elements presence on the page so it stands out. You can toggle this on and off using javascript. Basically just "highlight" form elements a user makes their way through it. Just another shameless plug for my idea. :)

Link to comment
Share on other sites

End User, wirehopper, thescientist, Thanks again for your inputs.Nothing wrong with a harmless plug now and then, I did think of that or rather using a "call out box" but I thought the other way would be much cooler.I may just have to go with the re-designing but I'll try to play around with the window properties first, who knows, I may learn something; imagine that.Peace,Elemental

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...