Jump to content

Onfocus And Tab


Dfrtbx

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>

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. :)

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

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...