Updating Content with Javascript: A Step-by-Step Guide

How can we modify the function show(question, answer) to handle single quotes?

function updateText(post)
{        
  let newQuestion = post.question.replace(/'/g,'\\'');       
  let newAnswer = post.answer.replace(/'/g,'\\'');               
  document.getElementById("question").innerHTML = newQuestion;  
  document.getElementById("answer").innerHTML = newAnswer;      
}

Answer №1

$(document).ready(function(){
  var inquiry = "this is a brand new question coming from me";
  var response = "this is a fresh answer provided by me";
  displayContent(inquiry, response);
});

function displayContent(inquiry, response) {
  document.getElementById("question").innerHTML = ReplaceText(inquiry,"'","\\'");
  document.getElementById("answer").innerHTML = ReplaceText(response, "'", "\\");
}

function ReplaceText(content, oldString, newString) {        
  return content.split(oldString).join(newString);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="question"></div>
<div id="answer"></div>

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

Leverage Axios in React to dynamically fetch and display real-time data

Executing an Axios get request in order to retrieve data and display it using React. export function Wareh() { const [wareh, setWareh] = useState([{}]); useEffect(() => { axios.get("http://localhost:1515/wareh").then((response) => ...

How can I fix the 'Null is not an object' error that occurs while trying to assess the RNRandomBytes.seed function?

Currently, I am in the process of creating a mobile application using expo and react-native. One of the features I am working on involves generating a passphrase for users on a specific screen. To achieve this task, I have integrated the react-native-bip39 ...

What steps can I take to resolve the issue of the "self signed certificate in certificate chain" error while trying to install plugins on VS Code?

After setting up VS Code on my Windows 7 x64 system, I encountered an issue when trying to install plugins - I kept receiving the error message "self signed certificate in certificate chain". Despite setting "http.proxyStrictSSL": false, I was still unable ...

Looking to capture all page requests in Nextjs 13 with the app router?

Back in Next.js 12, I was able to use the old Pages router and write pages/[...urlParts]/index.js, which would specifically catch page routes. However, now in Next.js 13 with the new App router, my app/[...urlParts]/page.js route is capturing everything, i ...

Similar to LINQ's Enumerable.First(predicate) method but with a slightly different syntax, this

When working with JavaScript, we often encounter situations where we need to find the first matching element based on certain conditions. Take for example this code snippet: function process() { var firstMatch = ['a', 'b', 'c&ap ...

Tips for resolving the error "React import attempt":

I'm a beginner in learning React and I encountered this error when trying to export NavigationMenu and import it into Navigation: Failed to compile ./src/components/Navigation.js Attempted import error: 'NavigationMenu' is not exported from ...

I am attempting to pass information through the body of an Axios GET request to be used in a Django backend, but when I try to print the request.body

As reported by Axios, it seems that this is a feasible solution: https://github.com/axios/axios/issues/462#issuecomment-252075124 I have the code snippet below where pos_title contains a value. export function getQuery(pos_code, id) { if (id === 94) ...

Having trouble pinpointing a particular hidden field within the Ajax response

My main focus is on the backend, but I am attempting to achieve a task on the frontend using Jquery. Here is what I am working on: Sending a URL from the backend in an Ajax response. Extracting and processing the URL from the Ajax response for further act ...

Tips for utilizing promises to create automated waiting for a function's completion in node.js/javascript

When I instantiate a module, it triggers numerous asynchronous functions. var freader = new filesreader(); // <-- this triggers multiple async functions var IMG_ARRAY = freader.get_IMG_ARRAY(); // <-- i retrieve the array where content is store ...

Implementing a dynamic update of an HTML element's content with JSON data - Learn how!

My task involves creating a quiz application where I need to show the answers along with images of the choices stored in my JSON data. However, I encounter an error: Uncaught TypeError: Cannot set properties of null (setting 'src') when I attempt ...

An error occurred while trying to upload the image: Undefined property 'subscribe' cannot be read

Recently, I implemented a create post function that allows users to fill in the title, content, and upload an image. However, I encountered an issue where the progress bar fills up and the image gets uploaded to Firebase successfully, but it doesn't a ...

Using jQuery to crop an SVG view box

I'm currently working on a sticker website project for a client who requires the ability to crop an image within a specific SVG shape, resembling a Halloween face (shown below). The uploaded image should be displayed only within this shape while hidi ...

Tips for customizing the blinking cursor in a textarea

I am experimenting with creating a unique effect on my website. I have incorporated a textarea with transparent text overlaying a pre element that displays the typed text dynamically using JavaScript. This creates an illusion of the user typing in real-tim ...

The modal disappears when the user clicks on the Previous/Next buttons of the jQuery UI datepicker

Using the jQuery datepicker from https://jqueryui.com/datepicker/ along with the UIkit framework found at I'm trying to incorporate the datepicker within a form that is inside a modal window. The issue arises when the modal window disappears after i ...

Analyzing JavaScript code coverage through unit testing with Jenkins and SonarQube

We have successfully implemented Jenkins/SonarQube to enforce a requirement that any new code committed by developers must have at least 70% unit test code coverage for Java. However, when it comes to applying the same rule for JavaScript, we encountered s ...

What is the best way to update state for changing colors?

I have a specific condition that requires setting the state properly. However, when I attempt to do so using setColor within the if method, an error occurs - "Too many re-renders. React limits the number of renders to prevent an infinite loop." State: con ...

Is there an Angular counterpart to Vue's <slot/> feature?

Illustration: Main component: <div> Greetings <slot/>! </div> Subordinate Component: <div> Planet </div> Application component: <Main> <Subordinate/> </Main> Result: Greetings Planet! ...

Retain the contents of the shopping cart even when the page is refreshed

For a course project, I am recreating a grocery store website and need assistance on how to retain the shopping cart values even after refreshing the webpage. Please inform me if more information is required... <button type="button" id= ...

React - Stopping the Submit Action

Recently, I have been delving into React development. In my exploration, I have incorporated the Reactstrap framework into my project. However, I have encountered an issue where the HTML form submits when a button is clicked. Is there a way to prevent this ...

What is the best way to include temporary attributes on a mongoose object solely for the purpose of a response, without saving them to the database

I am looking to add extra temporary properties with additional data to the response but have encountered some difficulties. 'use strict'; var mongoose = require('mongoose'); var express = require('express'); var app = expres ...