Dfrtbx Posted November 26, 2009 Report Share Posted November 26, 2009 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 More sharing options...
MrFish Posted November 26, 2009 Report Share Posted November 26, 2009 What does your code look like?<input type="text" onFocus=", , ,"> ?and onBlur? Link to comment Share on other sites More sharing options...
jlhaslip Posted November 26, 2009 Report Share Posted November 26, 2009 (edited) <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 November 26, 2009 by jlhaslip Link to comment Share on other sites More sharing options...
gar onn Posted November 26, 2009 Report Share Posted November 26, 2009 (edited) <input type="text" onblur="blurstyle()" onfocus="focusstyle()" onkeyup="focusstyle()" onchange="blurstyle()" />should do the trick Edited November 26, 2009 by gar onn Link to comment Share on other sites More sharing options...
jeffman Posted November 26, 2009 Report Share Posted November 26, 2009 (edited) 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 November 26, 2009 by Deirdre's Dad Link to comment Share on other sites More sharing options...
jlhaslip Posted November 26, 2009 Report Share Posted November 26, 2009 Thanks for the Compliment, DD. If I recall correctly, you and/or JSG assisted with that snippet.Have a good Turkey Day. Link to comment Share on other sites More sharing options...
Dfrtbx Posted November 26, 2009 Author Report Share Posted November 26, 2009 (edited) 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 November 26, 2009 by Dfrtbx Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now