Jump to content

Onfocus And Tab


Dfrtbx
 Share

Recommended Posts

I'm using dynamic styles to change around the appearance of <input text ... /> fields when they're focused (similar to Youtube's main search bar). I've noticed that whenever these fields are focused using the "tab" key the event doesn't fire. Is there anyway to remedy this?Thanks!

Link to comment
Share on other sites

<script type="text/javascript">	window.onload = function() {	if (document.getElementById("input1")) {	var s = document.getElementById("input1");	var v = s.defaultValue;	s.onfocus = function() {if(s.value==v) s.value='';}	s.onblur= function() {if(s.value=='') s.value=v;}	}	}</script>

try that.It needs an input field with an ID of input1.

		  <form name="form1" id="form1" method="get" action="index.php">		  <input type="text" id="input1" name="terms" value="Search..." />		  <input type="hidden" name="p" value="search" />		  <input class=" button" type="submit" name="Submit" value="GO" />	  </form>

Edited by jlhaslip
Link to comment
Share on other sites

Folks, if you're going to give advice, it should be good advice. Best practice says to keep your event handler assignments out of your element tags. Put them in your scripts, the way jlhaslip shows you in Post #3. JavaScript does not belong in an element tag ever.And, no, it doesn't matter that it works. Browsers are designed for backwards compatibility. That fact should not encourage anyone to use the kind of code we wrote in the previous century.Have a nice Thanksgiving. :)

Edited by Deirdre's Dad
Link to comment
Share on other sites

Thank you for all your posts. Upon testing it this morning, I found that it does work when you tab into the field. I don't know how, because it most certainly did not work last night, and I haven't changed anything since then. Maybe it's just a bit of Thanksgiving Day magic...Oh, and thank you DD for your event handler advice. Prior to reading your post, I had been setting them in the elements, but I'll set them in the JS now =D

Edited by Dfrtbx
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...