Tips for preserving textarea content upon clicking outside the area with the help of ajax

I am new to using the Froala editor and I am currently working on a feature where I need to save text in the textarea of the editor when the user clicks outside of it using Ajax. The ID of the content editor is #id_content.

Here is a snippet of my form in Django:

<form method="POST" id="froala_form" class="PageForm _inputHolder">
    {% csrf_token %}
    <div class="_alignRight posR _saveBTNOut">
      <button type="submit" class="_btn btnPrimery">Save as Draft</button>
    </div>
</form>

Despite my efforts, I have not been able to come up with a suitable logic for this task.

function loadDoc(){
      var xhttp=new XMLHttpRequest();
      xhttp.onreadystatechange=function{
        if(this.readyState==4 && this.status==200){
          var x = document.getElementById("id_content");
          x.addEventListener("click", ajaxsavefunc);
        }
      };
      xhttp.open("GET",'url', true);
      xhttp.send();
}

function ajaxsavefunc(xhttp) {
     document.getElementById("froala_form").innerHTML = editor.html.get()
}

Answer №1

Here is an illustration of how to utilize the blur event (triggered when the user clicks outside a field) in jQuery.

In this example, an alert will be displayed with the value from the input field when the user clicks inside the field and then clicks outside of it.

<form method="post" action="" >
   <input type="text" id="test" value="123">
   <input type="submit">
</form>
$(function () {
  $('#test').blur(function (e) {
    e.preventDefault();
    var x = document.getElementById("test").value;
    var saveData = $.ajax({
      type: 'POST',
      url: "someaction.do?action=saveData",
      data: {input: x},
      dataType: "text",
      success: function(resultData) { alert("Save Complete") }
    });
  });
});

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

One or multiple web browsers set in the Browserslist of the project

I have recently started working with the Ionic framework and encountered some warnings while setting up my application. Can anyone help me with a solution? [ng] When starting the Ionic application, I received the following warnings: [ng] One or more browse ...

What is the inner workings behind server side rendering in Next.js?

I am seeking clarification on Server Side Rendering, specifically with Next.js. During server side rendering, I want to confirm the 'execution path' as follows: Client makes a request to the server for the webpage, which serves up an HTML only ...

Unable to pass multiple objects as props in Vue component causing issues

Seeking help on passing multiple props to a component using v-for: <my-component v-for="(item, index) in items"></my-component> The data structure 'items' consists of: items: { 1: { name: "Apple", color: "Red" }, 2: { name: "Ba ...

Tips for maintaining the selected radio button state after refreshing the JSP page: Ensuring that the radio button remains selected

I need help with refreshing a page after clicking on one of two radio buttons. I've tried multiple solutions but haven't been successful so far. Can someone assist me? <script> $(document).ready(function() { $(document).on('c ...

Issue with function execution in MVC after invoking from jstree

My jquery code is supposed to trigger the MVC function call: $(document).ready(function () { alert("ddddd"); $("#divJsTreeDemo").jstree({ "plugins": ["json_data"], "json_data": { "ajax": { "type": "POST", "url": "/W ...

Handler for stack trace errors and error handling for promises

Introducing my customized error handling function: function onError(message, source, lineno, colno, error) { sendRequestToSendMail(arguments) } window.onerror = onError In addition to that, I have asynchronous tasks utilizing promises and I aim to captur ...

Troubleshooting ASP.NET Content Page Error: jQuery Object Expected

Currently working on designing a personal ASP.NET Web page, I have encountered an issue with making a sticky div using jQuery. Despite all my other jQuery functions functioning properly, the sticky div seems to be causing trouble. stickydiv.js $(document ...

Issue: [ng:areq] The parameter 'TasksCtrl' should be a function, but it is currently undefined

I seem to be encountering an error and I'm struggling to identify the cause. Is there something I overlooked? js var app = angular.module('Todolist', []); app.controller('TasksCtrl', [ '$scope', function($scope) { ...

"Enhancing User Authentication with Firebase Email Verification in React Native

Does Firebase have a feature that allows me to verify if an email confirmation has already been sent to a user? I am able to check validation, but I need to determine whether the verification email has already been sent in order to decide if it needs to be ...

The JSON response from XHR begins with the word "for."

While examining a few Facebook XHR requests, I noticed that there is a cross-domain request with a JSON response that begins like this: for (;;); {/* JSON object */} Why does the response start with a for? I suspect it might be for security reasons. Can ...

Customizing the MUI X Sparkline: Incorporating the percentage symbol at the end of the tooltip data within the MUI Sparklinechart

Presented below is a SparklineChart component imported from MUI X: import * as React from 'react'; import Stack from '@mui/material/Stack'; import Box from '@mui/material/Box'; import { SparkLineChart } from '@mui/x-chart ...

Encountering difficulties while attempting to decode specific characters from API calls in Django

The response I am receiving from an API contains a character \x96 Upon making the API call, the following error is triggered: 'ascii' codec can't encode character u'\x96' in position 56: ordinal not in range(128) This ...

Module 'prompt-sync' not found

When attempting to take user input in Node.js using the prompt module, I encountered the following error message: node:internal/modules/cjs/loader:998 throw err; ^ Error: Cannot find module 'prompt-sync' Require stack: - D:\Code\C+ ...

Cordova Geolocation now displaying incorrect latitude and longitude values as NAN

Recently starting out with javascript and Cordova, I decided to develop a basic GPS app in Visual Studio 2015. The goal was simple: get my current position by clicking on the "CURRENT POSITION" button. Testing it in Firefox yielded positive results. Howev ...

Can Handlebar.js function without the use of Ajax?

I am looking to implement handlebar.js templates on my website in order to showcase a list of articles. However, I am concerned that by using handlebar.js, I will be restricted to calling my data through an API via ajax request. This could potentially ma ...

How can I adjust the time in a range slider using AngularJS?

Currently, I am utilizing a Slider with draggable range in angular js for time selection. The Slider can be found here: https://jsfiddle.net/ValentinH/954eve2L/. I aim to configure the time on this slider to span from 00.00 to 24.00, with a 10-minute inter ...

Error loading custom Javascript in MVC 4 view during the first page load

I'm working on an MVC 4 application that utilizes jQuery Mobile. I have my own .JS file where all the functionality is stored. However, when I navigate to a specific view and check the page source, I notice that all scripts files are loaded except fo ...

Unique validator for Yii framework with Ajax messaging

I am currently working on validating a form against the model in Yii while keeping ajax validation turned on. My goal is to ensure that a specific field remains unique in my database. However, I have encountered an issue where the ajax message does not dis ...

Obtaining JSON Data Using WinJS.xhr():

I'm encountering difficulties with retrieving chat messages using winjs.xhr: function getMessage() { var time = MESSAGE_RETURNED.unixtime; if (time == 0) { time= window.parent.SESSION.unixtime; } WinJS.x ...

Cleanse the email using express-validator, but only if it is recognized as an email format; otherwise, disregard

Currently, I am developing an API that requires users to input their username and password for authentication purposes (login functionality). Users have the option to enter their email, username, or mobile number. To ensure consistency, I need to normalize ...