How to define a default value for "input type=text" without using attribute 'value'?

Html

Html Problem Overview


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.

Html Solutions


Solution 1 - Html

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" />

Solution 2 - Html

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>

Solution 3 - Html

> 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?

Solution 4 - Html

You can use Javascript.

For example, using jQuery:

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

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

Solution 5 - Html

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>

Solution 6 - Html

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.

Solution 7 - Html

this is working for me

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

or

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionq0987View Question on Stackoverflow
Solution 1 - HtmlArjun TuliView Answer on Stackoverflow
Solution 2 - HtmlGuffaView Answer on Stackoverflow
Solution 3 - HtmlPekkaView Answer on Stackoverflow
Solution 4 - HtmlSLaksView Answer on Stackoverflow
Solution 5 - HtmlOrkhan AlikhanovView Answer on Stackoverflow
Solution 6 - HtmlChuck ConwayView Answer on Stackoverflow
Solution 7 - HtmldEaD_ hOlicXView Answer on Stackoverflow