Because when the button is pressed the text of the first field does not go to the second?

Asked

Viewed 29 times

0

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8s">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>

    <input id="text1" type="text"><br>
    <input id="text2" type="text">
    <button id="button" type="button">click</button>

    <script>
        let text1 = document.querySelector("#text1");
        let text2 = document.querySelector("#text2");
        let button = document.querySelector("#button");

        button.addEventListener("click", function() {
            text2.textContent = text1.textContent;
        });

    </script>
    
    </body>
</html>

I’m creating a code in javascript and it aims to grab the text of the first field and play in the second field when the button is clicked, but when I click the button none of this happens and also does not return any error in the console browser, what I’m doing wrong?

1 answer

0

Simple, basically not textContent, but value

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8s">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>

    <input id="text1" type="text"><br>
    <input id="text2" type="text">
    <button id="button" type="button">click</button>

    <script>
        let text1 = document.querySelector("#text1");
        let text2 = document.querySelector("#text2");
        let button = document.querySelector("#button");

         button.addEventListener("click", function() {
            text2.value = text1.value;
        });

    </script>

    </body>
</html>

textContent is used when the element is a String, in the case of input as it will receive some value, it must be handled as value

Browser other questions tagged

You are not signed in. Login or sign up in order to post.