I have this simple implementation of ghost text:
HTML code:
<div id="searchPanel"> <form method="get" id="searchBox" action="somePage.php"> <input class="ghText" type="text" name="query" value="search here"/> </form> </div>
JQuery Code:
$(document).ready(function(){ $txtField = "#searchPanel form input.ghText"; var value = $($txtField).val(); $($txtField).focus(function(){ if($(this).val() == value) $(this).val("").removeClass("ghText"); }); $($txtField).blur(function(){ if($(this).val()==""){ $(this).val(value).addClass("ghText"); } }); });
The above example will not work. When the user focuses the cursor on the search bar, the "ghText" class is not deleted for some reason.
However, now, if I changed the value of "var" (variable initialization) and "value" with "$ value", as in:
$value = $($txtField).val(); $(this).val($value).removeClass("ghText"); $(this).val($value).addClass("ghText");
everything works perfectly.
I can just fall asleep and not worry too much about it ... but I'm very curious why something like this could happen?
is it because "this" does not refer to the right object, or is it because I tried to save the jQuery object in a variable other than jQuery, or is it about something else. Can anyone point me to what was wrong? I always thought that "var x" matches "$ x" ..?
javascript jquery
Benny Tjia Jun 09 2018-11-11T00: 00Z
source share