HTML URL value Property: URL Input Value

June 19, 2025

HTML URL value Property: Managing URL Input Values

The HTML URL value property is a crucial attribute of the <input type="url"> element, enabling developers to manage and manipulate the URL value entered by users in web forms. It allows you to programmatically set, retrieve, or modify the URL within the input field, providing enhanced control over form data.

What is the value Property?

The value property represents the current URL value of the URL input field. It can be used both to set a default URL or to retrieve the URL that the user has entered. This property is essential for handling form submissions, validating user input, and dynamically updating the URL field based on other actions within the webpage.

Syntax

The syntax for using the value property in JavaScript is straightforward:

  • Getting the value:
const urlValue = document.getElementById("urlInputId").value;
  • Setting the value:
document.getElementById("urlInputId").value = "https://www.example.com";

Attributes

The value property doesn’t have any specific HTML attributes associated with it beyond the standard <input> attributes. It is primarily manipulated through JavaScript.

Attribute Description
`value` Represents the current URL value in the input field. It can be read to retrieve the current value or set to change the value.

Examples

Let’s explore several practical examples that illustrate how to use the value property effectively with URL input types.

Basic Usage: Setting and Retrieving URL Values

This example demonstrates how to set a default URL value and retrieve it using JavaScript.

<label for="urlInputBasic">Enter URL:</label>
<input type="url" id="urlInputBasic" name="urlInputBasic" value="https://www.codelucky.com" /><br /><br />
<button id="getUrlButtonBasic">Get URL Value</button>
<p id="urlValueBasic"></p>

<script>
  const getUrlButtonBasic = document.getElementById("getUrlButtonBasic");
  getUrlButtonBasic.addEventListener("click", function () {
    const urlInputBasic = document.getElementById("urlInputBasic");
    const urlValueBasic = urlInputBasic.value;
    document.getElementById("urlValueBasic").textContent =
      "URL Value: " + urlValueBasic;
  });
</script>

Output:

After clicking the “Get URL Value” button, the paragraph element will display:

URL Value: https://www.codelucky.com

Clearing the URL Input Field

This example shows how to clear the URL input field using the value property.

<label for="urlInputClear">Enter URL:</label>
<input type="url" id="urlInputClear" name="urlInputClear" value="https://www.example.com" /><br /><br />
<button id="clearUrlButton">Clear URL Value</button>

<script>
  const clearUrlButton = document.getElementById("clearUrlButton");
  clearUrlButton.addEventListener("click", function () {
    const urlInputClear = document.getElementById("urlInputClear");
    urlInputClear.value = "";
  });
</script>

Output:

After clicking the “Clear URL Value” button, the URL input field will be cleared.

Validating and Setting URL Values

This example validates a URL entered by the user and sets it only if it is a valid URL format.

<label for="urlInputValidate">Enter URL:</label>
<input type="url" id="urlInputValidate" name="urlInputValidate" /><br /><br />
<button id="validateUrlButton">Validate URL</button>
<p id="urlValidationResult"></p>

<script>
  const validateUrlButton = document.getElementById("validateUrlButton");
  validateUrlButton.addEventListener("click", function () {
    const urlInputValidate = document.getElementById("urlInputValidate");
    const urlValueValidate = urlInputValidate.value;
    const urlValidationResult = document.getElementById("urlValidationResult");

    try {
      new URL(urlValueValidate);
      urlValidationResult.textContent = "Valid URL";
    } catch (error) {
      urlValidationResult.textContent = "Invalid URL";
      urlInputValidate.value = ""; // Clear the invalid URL
    }
  });
</script>

Output:

Depending on whether the entered URL is valid or invalid, the paragraph element will display:

  • For a valid URL: Valid URL
  • For an invalid URL: Invalid URL, and the input field will be cleared.

Dynamically Updating URL Values

This example shows how to dynamically update the URL value based on user interaction with other form elements.

<label for="urlInputDynamic">Base URL:</label>
<input type="url" id="urlInputDynamic" name="urlInputDynamic" value="https://www.example.com/" /><br /><br />

<label for="pathInput">Path:</label>
<input type="text" id="pathInput" name="pathInput" value="page1" /><br /><br />

<button id="updateUrlButton">Update URL</button>
<p id="updatedUrlValue"></p>

<script>
  const updateUrlButton = document.getElementById("updateUrlButton");
  updateUrlButton.addEventListener("click", function () {
    const urlInputDynamic = document.getElementById("urlInputDynamic");
    const pathInput = document.getElementById("pathInput");
    const updatedUrlValue = document.getElementById("updatedUrlValue");

    const baseUrl = urlInputDynamic.value;
    const path = pathInput.value;
    const fullUrl = baseUrl + path;

    urlInputDynamic.value = fullUrl; // Update the URL input
    updatedUrlValue.textContent = "Updated URL: " + fullUrl;
  });
</script>

Output:

After clicking the “Update URL” button, the URL input field will be updated with the combined base URL and path, and the paragraph element will display the updated URL.

For example, if the Base URL is https://www.example.com/ and the Path is page1, the output will be:

Updated URL: https://www.example.com/page1

Using the value Property in a Form Submission

This example demonstrates how to use the value property to capture the URL entered by the user upon form submission.

<form id="urlForm" action="#" method="GET">
  <label for="urlInputSubmit">Enter URL:</label>
  <input type="url" id="urlInputSubmit" name="urlInputSubmit" value="https://www.example.com" /><br /><br />
  <button type="submit">Submit</button>
</form>

<p id="formSubmissionResult"></p>

<script>
  const urlForm = document.getElementById("urlForm");
  urlForm.addEventListener("submit", function (event) {
    event.preventDefault(); // Prevent the form from actually submitting

    const urlInputSubmit = document.getElementById("urlInputSubmit");
    const urlValueSubmit = urlInputSubmit.value;
    document.getElementById("formSubmissionResult").textContent =
      "Submitted URL: " + urlValueSubmit;
  });
</script>

Output:

After submitting the form, the paragraph element will display:

Submitted URL: https://www.example.com

Note: Remember to prevent the default form submission behavior using event.preventDefault() in JavaScript to handle the form data as needed. 💡

Tips and Best Practices

  • Always Validate: Validate the URL entered by the user to ensure it is in the correct format before using it in your application.
  • Handle Empty Values: Properly handle cases where the URL input field is empty to prevent errors or unexpected behavior.
  • Use Labels: Always associate a label with your URL input field to provide clear instructions to the user.
  • Consider Accessibility: Ensure that your URL input fields are accessible by providing appropriate ARIA attributes and keyboard navigation support.

Browser Support

The <input type="url"> and its value property are supported by all modern browsers.

Conclusion

The HTML URL value property is a fundamental tool for handling URL input fields in web forms. By understanding how to use this property effectively, you can manage, validate, and manipulate URL values, enhancing the user experience and ensuring data integrity. Whether you’re setting default values, clearing input fields, or dynamically updating URLs, the value property offers the control you need for creating robust web applications.