Click event listener not working properly

What I try to make here is to make the user input in the text form appear as a quote on the page. I tried to use the click event listener but the result is only happened only during the click happen and not maintain.

Here is my HTML code:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="new2.css">
    <div class="header" id="header">
        <h2 class="title" id="title">QuotePersonalize</h2>
    <div class="body" id="body">
        <form class="user" id="user">
            <label for="quote">Enter your quote: </label>
            <input type="text" id="quote" value="quote"><br>
            <label for="author">Enter your name: </label>
            <input type="text" id="author" value="name">
            <button class="submit" id="submit" onclick="submission()">submit</button>
    <div class="res" id="res">
        <p class="result" id="result"></p>
    <script type="text/javascript" src="new2.js"></script>

//Here is my js file

const sub = document.getElementById('submit');
const quote = document.getElementById('quote').value;
const result = document.getElementById('result');
sub.addEventListener('click', e => {
    result.innerHTML = quote;

Where is this submission function?

I’ve copied your code in codepen, having bunch of errors in the console there:

Uncaught SyntaxError: Invalid or unexpected token 
Uncaught ReferenceError: submission is not defined 
Uncaught SyntaxError: Invalid or unexpected token 
1 Like

where is your new2.js file
please share the same

1 Like

As @admit8490 points out, you reference a function named submission in your html for the button’s onclick attribute, but there is no such function named in the JavaScript code you posted.

You already added a click event listener on the button in your JavaScript code, so you can delete the onclick="submission()" anyway. To get the quote to show up when you click the button, you need to read about the Event.preventDefault() method to avoid submitting the form (which will just reload the page based on your current form code) before having a chance to display the quote.

1 Like

thank you very much for pointing out the error from my code. Really helps me to improve. :grinning:

@KeshavMaheshwari new2.js is the same as the js file mentioned at the bottom of the topic. :grinning:

Thank you for the solution given. I have no idea this method exists before. Really help me a lot.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.