I have the following function:
$(document).ready(function() {
$("#dSuggest").keypress(function() {
var dInput = $('input:text[name=dSuggest]').val();
console.log(dInput);
$(".dDimension:contains('" + dInput + "')").css("display","block");
});
});
For some reason, for the first keypress, I’m getting an empty string to the console log.
This is because
keypressevents are fired before the new character is added to thevalueof the element (so the firstkeypressevent is fired before the first character is added, while thevalueis still empty). You should usekeyupinstead, which is fired after the character has been added.Note that, if your element
#dSuggestis the same asinput:text[name=dSuggest]you can simplify this code considerably (and if it isn’t, having an element with a name that is the same as theidof another element is not a good idea).