I have some bug with the following page: http://jsbin.com/agedu/ Source code with some comments: http://jsbin.com/agedu/edit
The problem is that when typing something and doing the query to display search results, if I go back to the search page in my browser (Firefox 3.5 but it's the same thing with IE8) there isn't my search terms anymore.
It's replaced by grey text. This grey text that I only want to have when there isn't any filled text.
When I remove the jQuery code, if I do some search and press "go back" button on my browser the filled text is still present.
And even with this example page: http://mucur.name/system/jquery_example/
If I write some text, load some other URL in the address bar, and then press go back button, the filled text is still present, while it's not with my code.
So my question is: do you have any idea how to keep this text if filled?
There should be a way to detect if the input is filled and avoid replacing text if so.
It may come from browsers and how they deal with JavaScript but I'm not sure about it.
Tested in IE8 and FF
This is copied from the source and modified.
I made a ghosted text control in plain old js a while back, looking at it in jQuery makes me want to go back and rewrite it.
When you initialize your plugin, you were setting resetting the value of the inputs, I just refactored a bit and added the following check:
I separated that operations from your focus event connection chain.
Now it will only set the default value (hvalue) and the default gray class (hclass) if the element value is
''
or it has the default value.Check your snippet here.
Wow, it took a long time to debug this one, I think you should use the val method instead of using the 'value' attribute.
Anyway, the problematic line is this
In the above line, when you assign the attribute 'value', you are actually changing the value of the text box. You should change it only if it's non empty.
I changed your code a little bit to use val() method everywhere and it works as you expected.
Working demo: http://jsbin.com/ihegu/
[Notice how the demo correctly greys out 'Wikipedia' text when you search something on Google and click on the back button.]
The inputdynvalue() function explicitly forces the textbox text when called, even if the textbox is non-empty.
Fix it like so:
The key is the lines:
which replace the un-conditional overwrite from before.
The fixed version can be seen at http://jsbin.com/ikipi.