Wrong textarea.value while making ajax calls to server side

I have a text area with id “text” and I am toggling the text area to appear on the screen with a click event on some div and I have 30 such divs.
Initially , I’m assigning the textarea.value with result of ajax call to my fetch api which fetches the data from the mongo on the server side based on an unique id.

Sometimes , when I’m making the ajax call to my update api in my backend , the textarea.value I’m sending as data to this ajax call is not the same as the updated text of the text area.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>//client side
// called when any of the divs is clicked
$(".radius").on("click", function(event) {
//extracting the id from the class and using this id as the id of my data for my mongo
var st=event.target.classList[1].substring(0,7);
var num=parseInt(event.target.classList[1].substring(7));
var toadd="close-button"+num;
//console.log(num+"modal")
closeButton.classList.add(toadd);
$.ajax({type: "POST",
url: "/fetch",
async: true,
data: JSON.stringify({
id: num,
}),
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success:function(result) {
input.value=result.text;
},
error:function(result) {
console.log("error")
}
});
modal.classList.toggle("show-modal");
});
// called when textarea is closed
function toggleModal1(event) {
var s1=closeButton.classList[closeButton.classList.length-1];
var st=s1.substring(12);
closeButton.classList.remove(s1);
var num=parseInt(st);
// event.preventDefault();
console.log(input.value)
$.ajax({type: "POST",
url: "/update",
data: JSON.stringify({
id:num,
text:input.value,
//input is my textarea
}),
dataType: 'json',
contentType: 'application/json; charset=utf-8',
success:function(result) {
},
error:function(result) {
console.log("error")
}
});
modal.classList.toggle("show-modal");
}
</code>
<code>//client side // called when any of the divs is clicked $(".radius").on("click", function(event) { //extracting the id from the class and using this id as the id of my data for my mongo var st=event.target.classList[1].substring(0,7); var num=parseInt(event.target.classList[1].substring(7)); var toadd="close-button"+num; //console.log(num+"modal") closeButton.classList.add(toadd); $.ajax({type: "POST", url: "/fetch", async: true, data: JSON.stringify({ id: num, }), dataType: 'json', contentType: 'application/json; charset=utf-8', success:function(result) { input.value=result.text; }, error:function(result) { console.log("error") } }); modal.classList.toggle("show-modal"); }); // called when textarea is closed function toggleModal1(event) { var s1=closeButton.classList[closeButton.classList.length-1]; var st=s1.substring(12); closeButton.classList.remove(s1); var num=parseInt(st); // event.preventDefault(); console.log(input.value) $.ajax({type: "POST", url: "/update", data: JSON.stringify({ id:num, text:input.value, //input is my textarea }), dataType: 'json', contentType: 'application/json; charset=utf-8', success:function(result) { }, error:function(result) { console.log("error") } }); modal.classList.toggle("show-modal"); } </code>
//client side 
// called when any of the divs is clicked
$(".radius").on("click", function(event) {
//extracting the id from the class and using this id as the id of my data for my mongo
var st=event.target.classList[1].substring(0,7);
var num=parseInt(event.target.classList[1].substring(7));
var toadd="close-button"+num;
//console.log(num+"modal")
closeButton.classList.add(toadd);
$.ajax({type: "POST",
        url: "/fetch",
        async: true,
        data:  JSON.stringify({
            id: num,
        }),
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        success:function(result) {
          input.value=result.text;
        },
        error:function(result) {
           console.log("error")
        }
});
modal.classList.toggle("show-modal");
});
// called when textarea is closed
function toggleModal1(event) {
    var s1=closeButton.classList[closeButton.classList.length-1];
    var st=s1.substring(12);
    closeButton.classList.remove(s1);
    var num=parseInt(st);
   // event.preventDefault();
   console.log(input.value)
    $.ajax({type: "POST",
        url: "/update",
        
        data:  JSON.stringify({
            id:num,
            text:input.value,
  //input is my textarea
        }),
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        success:function(result) {
           
        },
        error:function(result) {
           console.log("error")
        }
    });
    modal.classList.toggle("show-modal");
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>//server side
app.post("/fetch",function(req,res)
{
//console.log(req.body);
// var id1=req.body.id;
const findInDB= Fruit.findOne({id:req.body.id},function (err, docs) {
console.log(docs);
res.send({text:docs.text});
});
});
app.post("/update",function(req,res)
{
Fruit.updateOne({id:req.body.id},
{text:req.body.text}, function (err, docs) {
if (err){
console.log(err)
}
else{
console.log("Updated Docs : ", docs);
}
});
</code>
<code>//server side app.post("/fetch",function(req,res) { //console.log(req.body); // var id1=req.body.id; const findInDB= Fruit.findOne({id:req.body.id},function (err, docs) { console.log(docs); res.send({text:docs.text}); }); }); app.post("/update",function(req,res) { Fruit.updateOne({id:req.body.id}, {text:req.body.text}, function (err, docs) { if (err){ console.log(err) } else{ console.log("Updated Docs : ", docs); } }); </code>
//server side
app.post("/fetch",function(req,res)
{
      //console.log(req.body);
      // var id1=req.body.id;
      const findInDB= Fruit.findOne({id:req.body.id},function (err, docs) {
        console.log(docs);
             res.send({text:docs.text});
       });  
       
});
app.post("/update",function(req,res)
{
  Fruit.updateOne({id:req.body.id}, 
    {text:req.body.text}, function (err, docs) {
    if (err){
        console.log(err)
    }
    else{
       console.log("Updated Docs : ", docs);
    }
});

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật