1

I`m trying to read the value from an input and fill it in one empty div.However , the value is being read but when I click the button submit , the value appears for like 0.2 seconds on the empty div , and then , disappears ... any suggestions why?

HTML :

 <div id="vuvedenaSuma">

 </div>

        <form action="">
            <input type="text" id="suma" required="required" placeholder="Въведи сума" name="Suma" />

            <button id="submit">Submit!</button>
        </form>

Javascript:

function valueRead(){
    var vuvedenaSuma = document.getElementById('suma').value;
    document.getElementById('vuvedenaSuma').innerHTML = vuvedenaSuma;
}

document.getElementById('submit').addEventListener('click',valueRead);

I want to make it withe eventListener , not onclick attribute on the button.

5 Answers 5

3

Your form is being submitted right after the execution of the function.

You can prevent the default event(form submission) to be called with event.preventDefault() like this:

function valueRead(e){
    var vuvedenaSuma = document.getElementById('suma').value;
    document.getElementById('vuvedenaSuma').innerHTML = vuvedenaSuma;
    e.preventDefault();
}

https://jsfiddle.net/ez0qchyq/

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

Comments

3

Your event is firing, then the page is likely reloading because of the form firing. Try using

event.preventDefault()

and it will prevent the form from submitting.

Edit: Also, the comment below me is absolutely correct. Remember to pass the event into the function.

1 Comment

For browser support, be sure to pass the event parameter to the valueRead function. Example: document.getElementById('submit').addEventListener('click',valueRead(event));
2

The default way that a form "submits" is to send a new page request to the server, using the given inputs as parameters. From there, a serverside language like PHP might save them. Often, this would churn out an "Operation successful!" page or similar.

In your case, your form's action is blank, meaning it will "submit" to the page it's on. Since your page is pretty basic, it will reload without any of the sent information appearing in it.

As John Kossa suggested, you could intercept this by adding an argument, let's say, "evt", to the parentheses of the valueRead function, and then calling evt.preventDefault().

Comments

1

When you click the button then you send the form and the page is automatically refreshed. To prevent this behavior try this solution

function valueRead(e){
    e.preventDefault();
    var vuvedenaSuma = document.getElementById('suma').value;
    document.getElementById('vuvedenaSuma').innerHTML = vuvedenaSuma;
}

Comments

1

Also, you might want to use the event listener on submit:

document.getElementById('formName').addEventListener('submit', valueRead);

Comments

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.