Insert notes into the div employing only vanilla JavaScript

Is there a way to dynamically add comments to a div element using JavaScript or jQuery without reloading the page?

Here is an example scenario:

Suppose I have a form:

<form action="#" id="create" method="post">
    <fieldset>

    <legend><strong>Add your comment</strong></legend>

    <p>
        <label for="author">Name <abbr title="Required">*</abbr></label>
        <input name="author" id="author" value=""
        required="required" aria-required="true"
        pattern="^([- wdu00c0-u024f]+)$"
        title="Your name"
        type="text" spellcheck="false" size="20" />
    </p>

    <p>
        <label for="email">Email <abbr title="Required">*</abbr></label>
        <input name="email" id="email" value=""
        required="required" aria-required="true"
        pattern="^(([-wd]+)(.[-wd]+)*@([-wd]+)(.[-wd]+)*(.([a-zA-Z]{2,5}|[d]{1,3})){1,2})$"
        title="Your email address"
        type="text" spellcheck="false" size="30" />
    </p>

    <p>
        <label for="text">Comment <abbr title="Required">*</abbr></label>
        <textarea name="text" id="text"
        required="required" aria-required="true"
        title="Your comment"
        spellcheck="true" cols="40" rows="10"></textarea>
    </p>

    </fieldset>
    <fieldset>

    <button name="preview" type="submit">Preview</button>
    <button name="save" type="submit">Submit Comment</button>

    </fieldset>

</form>

<div id="comments"></div>

In this case, I want to submit the form and add the content of the textarea to the div element next to the form without refreshing the page.

Any suggestions on how to achieve this functionality?

Answer №1

When working with JavaScript directly:

document.getElementById('messages').innerHTML = 'Message delivered!';

If using jQuery library:

$('#messages').html('Message delivered!');

Answer №2

Insert the following javascript function into the webpage's code:

<script>
 function displayMessage()
  {  
   document.getElementById("message").innerHTML = document.getElementById("inputText").value;
  }
</script>

Invoke this function when the "Click Here" button is clicked:

<button name="submit" type="button" onclick="displayMessage()">Click Here</button>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Consolidate all CSS links into a single line

Within my HTML document, I currently have the following CSS links: myPage.html <link href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,500,500i,700,700i" rel="stylesheet"> <link href="https://fonts.googleapis.com/icon?family ...

Tips for creating an endless loop within a nested v-for in Vue?

Here is my attempt at solving the problem: HTML <ul class="tree"> <li> <span>First Node</span> <ul> <li v-for="(tree, i) in trees" :key="i"> <span v-text="tree. ...

After making an Ajax call using AngularJS in a PHP file, I expected to receive only a success or fail message. Instead, I received the entire HTML page code along

After making an Ajax call using AngularJS in a PHP file, I expected to receive only a success/fail message. However, I ended up receiving the full HTML page code along with tags. Here is my AngularJS code: $http.post('ajax_Location.php',{ &apos ...

Mobile devices are unable to properly implement the Jquery show/hide feature

Currently, I am working on a small Russian project and facing some challenges with the mobile version of my website: php8098.github.io/breakfast. The issue lies within the first slider where the play button should trigger a modal window to open. I urgentl ...

Bootstrap creates an element positioned at the end of the row

I am currently utilizing the Bootstrap framework and I'm facing a challenge where I need an element positioned at the end of a row. <div class="row"> <div>div 1</div> <div>div end</div> </div> These divs a ...

Dealing with the click event of a Submit Button

Below is the code I am using to handle the click event of the submit button: $('#subbtn').on("click",function(){ var stcode = $('.stcode11').val(); var sem = $('.sem11').val(); var doa = $('.doa11').val( ...

Utilizing Javascript Objects on Websites

Beginner's query: Even after researching extensively on various platforms like W3Schools and other websites, I am unable to find the solution I need. I am sure it is a simple fix for someone experienced like you. I am working with ASP.Net and wish t ...

Enhance your Vue.js experience with an improved method for monitoring values

My setup involves Vue CLI 3.11 (based on Node.js 12.10) and Google Chrome. I am trying to create a syntax that can bypass the watch function under specific conditions. However, I am struggling because Vue.js handles value changes differently compared to J ...

What techniques can I use to streamline this jQuery script?

Presented below is the code for a straightforward newsletter signup widget. I believe there's potential to streamline it further, any suggestions? const emailForm = $('.widget_subscribe form'); const emailSubmit = $('.widget_subscrib ...

Exploring the ins and outs of flexbox and utilizing overflow:auto

In my current situation, I have successfully resolved all cases but am now seeking assistance in debugging my mental model. Specifically, I am focusing solely on Chrome for the sake of simplicity. Within my nested "holy grail-ish" flexbox layouts, I have ...

Is it possible to verify if a user is accessing a webpage through Electron?

If I were interested in creating a basic Electron application that notifies the user upon reaching example.com, is this achievable? If yes, then how can I determine if the user is on a particular webpage? ...

Ways to invoke external jQuery functions within Angular applications

In the midst of working on an Angular CLI Project, I find myself needing to incorporate some jQuery functionalities. To accomplish this task, I have added the necessary JavaScript files in the angular.json configuration: "scripts": [ "node_modules/jq ...

Trigger a callback in ASP.NET's CallbackPanel using JavaScript every 10 seconds

I am attempting to automatically trigger the callback of a CallbackPanel using JavaScript in my WebFormUserControl every 10 seconds. While I can trigger it with an ASPxButton and ClientSideEvents, my ultimate aim is to have it start automatically every 10 ...

Adding a cell break line within AG-GRID in an Angular application

I'm trying to display two sets of data in a single cell with ag-grid, but I want the data to be on separate lines like this instead: Data with break line I attempted to use "\n", "\r", and "\br" but it didn't work. Here is my code ...

Creating a shared singleton instance in Typescript that can be accessed by multiple modules

Within my typescript application, there is a Database class set up as a singleton to ensure only one instance exists: export default class Database { private static instance: Database; //Actual class logic removed public static getInstance() ...

The `stream.Transform.unshift()` method in Node.js

Let's take a look at this simple example: stream = require 'stream' util = require 'util' class TestTransform extends stream.Transform _transform: (chunk, encoding, callback) -> if not @noMore @noMore ...

inputting an array of strings into the value attribute of an input element using jQuery

Whenever a user writes something in the input field with id="clientAdd" and presses enter, that text should be displayed as a tag inside a div. These tags are then saved as an array of strings, which I want to pass as the value for the hidden input field w ...

Using form submission to implement reCAPTCHA v3 in g-recaptcha

Is the Recaptcha API causing trouble? I have the key on both the submit button and the form tag, but it's only sending the sitekey without generating tokens. Any suggestions are welcome. You can either use the key in the form tag: <form method=&qu ...

Is there a way to incorporate GIFs into a Node.js art generation script that anyone knows of?

I've been working on a node.js code that generates art by combining different layers using the PIL library. One of the layers I want to use is a gif, but for some reason, when I apply it, it shows up as a regular image even though the file is a gif. A ...

Just work seamlessly with comparable features

Is there a more efficient way to simplify this function? I am faced with multiple tweens that share similar ease properties. While I acknowledge the broad nature of this query, I have yet to discover a satisfactory solution. $('input').chan ...