0

very small problem...

PROBLEM: when I run the code it does not show the input field value into div but why?

REQUIREMENT: my requirement is that the first user enters the value in the input field then when I click on the button the input value is display on the div.

CODE:

 function abc(){
            window.setInterval(()=>{
                var myForm = document.getElementById('myForm');
                var text = document.getElementById('text');
    
                text.innerText = myForm.elements['email'].value;
            }, 1);        
        }
<form id="myForm">
            <input type="number" name="email" value="[email protected]">
            <button id="btn" onclick="abc()">click On me</button>
        </form>
        <p id='text'></p>

1 Answer 1

1

You do not need "setInterval" for this action

When the button element is in form element, it defaults to the send function, which in this case refreshes the page. Thus, the contents of the field are cleaned as soon as it has been filled.

I removed the form element. Now, when a button is clicked, the contents of the input element are taken and displayed in the p element

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

    <input id="mail" type="text" name="email" value="[email protected]">
    <button id="btn" onclick="abc()">click On me</button>

    <p id='text'></p>

    <script>
        function abc() {
            var mail = document.getElementById('mail');
            var text = document.getElementById('text');
            text.innerText = mail.value;
        }
    </script>

</body>
</html>


Version with setInterval

After pressing the button "setInterval" will be activated. Every 100ms it will takes all the information from the input element and will fill it in the P element. You can write in the field and the text will immediately appear in the other element. In order not to accumulate spaces after pressing the button, I have set a line in the code that deactivates the button

function abc() {
    document.getElementById('btn').setAttribute("disabled", true);

    setInterval(() => {
        var mail = document.getElementById('mail');
        var text = document.getElementById('text');
        text.innerText = mail.value;
    }, 100);
}
<input id="mail" type="text" name="email" value="[email protected]">
<button id="btn" onclick="abc()">click On me</button>

<p id='text'></p>

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much bro
ok I got your point but you can solve the same problem with setinterval bcz that will help me to understand the concept of setinterval
I added a version with "setInterval" in my post. If my answer is useful to you, you can mark this post as Accepted Answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.