diff --git a/core/misc/machine-name.js b/core/misc/machine-name.js
index 6ac79f9100e49b266b714a11c6dfb066712c5c6d..86279e556eb440677cb4359451ff159039412a54 100644
--- a/core/misc/machine-name.js
+++ b/core/misc/machine-name.js
@@ -229,7 +229,7 @@
         // Preview the machine name in realtime when the human-readable name
         // changes, but only if there is no machine name yet; i.e., only upon
         // initial creation, not when editing.
-        if ($target[0].value === '') {
+        if (machine === '') {
           // Listen to the 'change' and 'input' events that are fired
           // immediately as the user is typing for faster response time. This is
           // safe because the event handler doesn't include any slow
diff --git a/core/tests/Drupal/FunctionalJavascriptTests/Core/MachineNameTest.php b/core/tests/Drupal/FunctionalJavascriptTests/Core/MachineNameTest.php
index 31f0441758602f011570ffb30ba5a9d1b78935ed..fd624d93183b969154649dbd6dc34ef4f411624a 100644
--- a/core/tests/Drupal/FunctionalJavascriptTests/Core/MachineNameTest.php
+++ b/core/tests/Drupal/FunctionalJavascriptTests/Core/MachineNameTest.php
@@ -89,6 +89,7 @@ public function testMachineName(): void {
 
     // Get elements from the page.
     $title_1 = $page->findField('machine_name_1_label');
+    $title_3 = $page->findField('machine_name_3_label');
     $machine_name_1_field = $page->findField('machine_name_1');
     $machine_name_2_field = $page->findField('machine_name_2');
     $machine_name_1_wrapper = $machine_name_1_field->getParent();
@@ -137,6 +138,14 @@ public function testMachineName(): void {
     // Validate if the element contains the correct value.
     $this->assertEquals(end($test_values)['expected'], $machine_name_1_field->getValue(), 'The ID field value must be equal to the php generated machine name');
 
+    // Assert that a machine name based on a default value is initialized.
+    $this->assertJsCondition('jQuery("#edit-machine-name-3-label-machine-name-suffix .machine-name-value").html() == "yet_another_machine_name"');
+
+    // Assert that a machine name based on a default value is updated when the
+    // value is updated.
+    $title_3->setValue('A new machine name');
+    $this->assertJsCondition('jQuery("#edit-machine-name-3-label-machine-name-suffix .machine-name-value").html() == "a_new_machine_name"');
+
     // Test that machine name generation still occurs after an HTML 5
     // validation failure.
     $this->drupalGet('form-test/machine-name');
@@ -147,9 +156,6 @@ public function testMachineName(): void {
     $this->assertNotEmpty($machine_name_2_value, 'Machine name field 2 must be initialized');
     $this->assertNotEmpty($machine_name_3_value, 'Machine name field 3 must be initialized');
 
-    // Assert that a machine name based on a default value is initialized.
-    $this->assertJsCondition('jQuery("#edit-machine-name-3-label-machine-name-suffix .machine-name-value").html() == "yet_another_machine_name"');
-
     // Test each value for conversion to a machine name.
     foreach ($test_values as $test_info) {
       // Set the value for the field, triggering the machine name update.