menu

Questions & Answers

Onkeypress doesn't detect first character

I am trying to remove watermark of text box on enter of first character I have written event of key press, after enter of key it returns length of zero on first character. I tried key up, problem with key up is when some one holds continuously the key the water mark doesn't hide:

<input type="text" autocomplete="off" id="txtHomeSearch"  onkeyup="javascript:CheckInputLength(this,'lblwatermark')"/>

function CheckInputLength(e,labelid) {
    if (e.value.length != 0) {
        $(labelid).className = 'NewHP_LabelSearch';
        $(labelid).hide();
    }
}

The water mark doesn't hide on onkeypress event.

Please help me out

Is there any event detects the on change of text box for first character and copy paste..

Answers(2) :

The keypress event happens before the key is accepted by the <input>. (People use this to prevent the <input> from accepting the event.) You want to use the input event, which fires after the value has changed.

change this onkeyup="javascript:CheckInputLength(this,'lblwatermark')" to this onchange="CheckInputLength(this,'#lblwatermark')"

You needed to add in a # character in the beginning to make it a valid id selector. Also the javascript: thing is not needed so took it out.

Edit: reread question. It looks like poster wants the onchange event for the input