78

I need to give a default value for input type=text field as follows:

<input type="text" size="32" value="" name="fee" />

There is one way to give this default value as I know:

<input type="text" size="32" value="1000" name="fee" />

Here is the question: Is it possible that I can set the default value without using attribute value?

As I know, if I set the enter the value 1000 manually, and then view the source through web browser the value is still empty. So I think there may be a method that I can use.

2
  • 35
    What's wrong with value?
    – SLaks
    Aug 31, 2010 at 15:28
  • @SLaks it's a bad idea for a programming language to reserve a keyword as ubiquitous as value. My guess is value here has a conflicting name with some other code. Mar 30, 2022 at 19:48

8 Answers 8

42

You should rather use the attribute placeholder to give the default value to the text input field.

e.g.

<input type="text" size="32" placeholder="1000" name="fee" />
6
  • as of this post, the placeholder pseudo-element is only partially supported: caniuse.com/#search=placeholder
    – Joe Maffei
    Aug 4, 2016 at 18:19
  • 6
    @JoeMaffei caniuse.com refers to CSS support. The placeholder attribute is fully supported by all browsers it's just a hassle to change the default styling of it.
    – Frits
    Oct 25, 2016 at 8:39
  • 113
    This doesn't answer the question. OP wants to set a default value. Placeholder doesn't set the value.
    – aandis
    Feb 22, 2018 at 18:32
  • 5
    default value and placeholder are totally different things...
    – yuan
    Jul 24, 2020 at 8:20
  • 7
    This just show the text as background, it doesn't set the default value.
    – rkachach
    Sep 24, 2020 at 9:10
22

Here is the question: Is it possible that I can set the default value without using attribute 'value'?

Nope: value is the only way to set the default attribute.

Why don't you want to use it?

1
17

You can change the name attribute by id, and set the value property using client script after the element is created:

<input type="text" id="fee" />

<script type="text/javascript">
document.getElementById('fee').value = '1000';
</script>
12

You can use Javascript.

For example, using jQuery:

$(':text').val('1000');

However, this won't be any different from using the value attribute.

4
  • 4
    That won't actually set the default value. The DOM property, defaultValue, would be necessary for that.
    – Andy E
    Aug 31, 2010 at 15:30
  • Actually, this is different from using the value attribute. View Source will not have value="1000" in it. Viewing rendered source still will, but regular source will not.
    – Ryan Kinal
    Aug 31, 2010 at 15:32
  • Yes, this is an alternative way. However it still doesn't fix my original problem. stackoverflow.com/questions/3606118/…
    – q0987
    Aug 31, 2010 at 15:32
  • @q087: See my answer to your other question; you have a different problem.
    – SLaks
    Aug 31, 2010 at 15:39
4

this is working for me

<input defaultValue="1000" type="text" />

or

let x = document.getElementById("myText").defaultValue; 
3
3

A non-jQuery way would be setting the value after the document is loaded:

<input type="text" id="foo" />

<script>
    document.addEventListener('DOMContentLoaded', function(event) { 
        document.getElementById('foo').value = 'bar';
    });
</script>
2

The value is there. The source is not updated as the values on the form change. The source is from when the page initially loaded.

0

Use defaultValue:

<input defaultValue="1000" type="text" />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.