Home > Net >  Prevent value from overriding placeholder - Inputs HTML
Prevent value from overriding placeholder - Inputs HTML

Time:06-21

I want to use a placeholder as a description for a input box. I also want the default value to be 50.

However, if value is set, it overrides the placeholder:

<input name="freq" type="number" placeholder="Frequency (Hz)" value="50">

Is there any way to have both the description and the default value?

Thanks in advance

CodePudding user response:

Try this:

<input name="freq" type="number" placeholder="Frequency (Hz)" onfocus="this.value=50">

If you want to display the placeholder, you cannot set value when the script is loaded.

However after the users have read the placeholder and attempt to input some value (i.e. click into the input element, which is triggered by the onfocus event), you can set the default value (by setting this.value which refer to the input element's value to your default).

CodePudding user response:

Having both value and placeholder is not possible. What you can do is to add the predefined text using css absolute to look it like a placeholder in the Input. Also pls check this stackoverflow

  •  Tags:  
  • html
  • Related