Javascript input set value

Sometimes we need to set a default value of the element, This example explains methods to do so.

    Text Value Property
    This property set/return the value of value attribute of a text field.
    The value property contains the default value, the value a user types or a value set by a script.
    Syntax:

    Return the value property:

Set the value property:

Property Values:

  • text:It specifies the value of input text field.
  • attributeValue:This parameter is required. It specifies the value of the attribute to add.
  • setAttribute method
    This method adds the specified attribute to an element, and set it’s specified value.
    If the attribute already present, then it’s value is set/changed.
    Syntax:
  • Parameters:

    • attributeName:This parameter is required. It specifies the name of the attribute to add.
    • attributeValue:This parameter is required. It specifies the value of the attribute to add.

    Example-1:This example sets the value of the input element to ‘textValue’ by Text Value property.

    On this Page

    The values() method returns a new Iterator object that contains the values for each element in the Set object in insertion order.

    The keys() method is an alias for this method (for similarity with Map objects); it behaves exactly the same and returns values of Set elements.

    The source for this interactive example is stored in a GitHub repository. If you’d like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

    Syntax

    Return value

    A new Iterator object containing the values for each element in the given Set , in insertion order.

    Читайте также:  Где режим инкогнито в яндекс браузере

    How would you set the default value of a form text field in JavaScript?

    12 Answers 12

    This is one way of doing it:

    if your form contains an input field like

    then you can write the code in javascript as given below to set its value as