OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Button/Function not working due to ' in the generated text

  • Thread starter Thread starter Ravish Agrawal
  • Start date Start date
R

Ravish Agrawal

Guest
I am building a response generator using gpt.

this is the function that creates a comment item

Code:
// Function to create a comment item
function createCommentItem(comment) {
    const li = document.createElement('li');
    li.innerHTML = `
        <input type="checkbox" class="comment-checkbox" data-comment-id="${comment.commentId}" data-comment-text="${comment.response}">
        <strong>${comment.author}:</strong> ${comment.text}
        <p><strong>Response:</strong> ${comment.response.replace(/'/g, "&#39;")}</p>
        <p><strong>Date:</strong> ${new Date(comment.publishedAt).toLocaleDateString()}</p>
        <button class="reply-btn">Reply</button>
    `;

    const replyButton = li.querySelector('.reply-btn');
    replyButton.addEventListener('click', () => replyToComment(comment.commentId, comment.response.replace(/'/g, "&#39;")));

    return li;
}

this is the function that generate the reply

Code:
// Function to reply to a single comment
async function replyToComment(commentId, text) {
    try {
        const response = await fetch('/reply-to-comment', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({ commentId, text })
        });
        if (response.ok) {
            alert('Reply posted successfully');
        } else {
            alert('Failed to post reply');
        }
    } catch (error) {
        console.error('Error posting reply:', error);
        alert('Error posting reply');
    }
}

When clicked on the Reply button this code should post the response on text on Youtube however response-1 doesn't appear on youtube but response-2 does

Response-1:

You're welcome! If you have any more questions or need further information, feel free to ask.

Response-2: (Note: this is hindi text, written in english language)

Haan, aksar ek question ke baad aur questions aate hain. Agar aapko kisi topic par detailed information chahiye toh aap multiple questions bhi pooch sakte hain. Main puri koshish karungi aapke sawalon ka jawab dena.

The ' is interfering with the code. I have tried using encoding, removing special characters, adding event listener separately but nothing seems to work.

<p>I am building a response generator using gpt.</p>
<p>this is the function that creates a comment item</p>
<pre><code>// Function to create a comment item
function createCommentItem(comment) {
const li = document.createElement('li');
li.innerHTML = `
<input type="checkbox" class="comment-checkbox" data-comment-id="${comment.commentId}" data-comment-text="${comment.response}">
<strong>${comment.author}:</strong> ${comment.text}
<p><strong>Response:</strong> ${comment.response.replace(/'/g, "&#39;")}</p>
<p><strong>Date:</strong> ${new Date(comment.publishedAt).toLocaleDateString()}</p>
<button class="reply-btn">Reply</button>
`;

const replyButton = li.querySelector('.reply-btn');
replyButton.addEventListener('click', () => replyToComment(comment.commentId, comment.response.replace(/'/g, "&#39;")));

return li;
}
</code></pre>
<p>this is the function that generate the reply</p>
<pre><code>// Function to reply to a single comment
async function replyToComment(commentId, text) {
try {
const response = await fetch('/reply-to-comment', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ commentId, text })
});
if (response.ok) {
alert('Reply posted successfully');
} else {
alert('Failed to post reply');
}
} catch (error) {
console.error('Error posting reply:', error);
alert('Error posting reply');
}
}
</code></pre>
<p>When clicked on the Reply button this code should post the response on text on Youtube however response-1 doesn't appear on youtube but response-2 does</p>
<p><strong>Response-1:</strong></p>
<blockquote>
<p>You're welcome! If you have any more questions or need further information, feel free to ask.</p>
</blockquote>
<p><strong>Response-2:</strong>
(Note: this is hindi text, written in english language)</p>
<blockquote>
<p>Haan, aksar ek question ke baad aur questions aate hain. Agar aapko kisi topic par detailed information chahiye toh aap multiple questions bhi pooch sakte hain. Main puri koshish karungi aapke sawalon ka jawab dena.</p>
</blockquote>
<p>The <code>'</code> is interfering with the code. I have tried using encoding, removing special characters, adding event listener separately but nothing seems to work.</p>
 
Top