89

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
  • 42
    What's wrong with value? Commented 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. Commented Mar 30, 2022 at 19:48

8 Answers 8

47

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" />
Sign up to request clarification or add additional context in comments.

7 Comments

@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.
This doesn't answer the question. OP wants to set a default value. Placeholder doesn't set the value.
default value and placeholder are totally different things...
This just show the text as background, it doesn't set the default value.
Placeholder is a hint to show what kind of value is expected not a "value"
|
28

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 Comment

Hello Pekka, Please see the related question: stackoverflow.com/questions/3606118/…
20

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>

Comments

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 Comments

That won't actually set the default value. The DOM property, defaultValue, would be necessary for that.
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.
Yes, this is an alternative way. However it still doesn't fix my original problem. stackoverflow.com/questions/3606118/…
@q087: See my answer to your other question; you have a different problem.
8

this is working for me

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

or

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

4 Comments

defaultValue is a React feature for managing uncontrolled inputs. Unfortunately, it doesn't work with ordinary HTML.
@spiffytech defaultValue has nothing to do with React, it's part of the HTML DOM API, see developer.mozilla.org/en-US/docs/Web/API/… or w3schools.com/jsref/prop_text_defaultvalue.asp.
It's standard as a DOM property that can be set at runtime, but it doesn't appear to be standard as an HTML element attribute. Here's a demo showing the element attribute doesn't work: codepen.io/spiffytech/pen/rNQywob
This does not work in Chrome or in Firefox in 2024
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>

Comments

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.

Comments

1

Use defaultValue:

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

1 Comment

You can't use defaultValue in html, you can only use it in javascript to set or get the default value (the value set in html with 'value="x"' of a text input, so for example you can compare it with value and you will know if that input changed its value.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.