Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6647709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:34:10+00:00 2026-05-26T00:34:10+00:00

I need to accept arbitrary user input. Later, I want to copy that input,

  • 0

I need to accept arbitrary user input. Later, I want to copy that input, already escaped to HTML entities, into a textarea so it can be edited. However, jQuery’s jQuery.html() function reads out the HTML entities as if they were real HTML and injects the user script directly into the page. I need to grab the contents of the user input, even if it contains <script> tags, and output to a textarea where it can be edited.

So, I need this:

<textarea name="empty"></textarea>
<div name="user-input"> &lt;script&gt; window.alert('hi'); &lt;/script&gt; </div>

to become this:

<textarea name="empty"> &lt;script&gt; window.alert('hi'); &lt;/script&gt; </textarea>
<div name="user-input"> &lt;script&gt; window.alert('hi'); &lt;/script&gt; </div>

on JavaScript trigger. Instead, the textarea becomes:

<textarea name="empty"> <script> window.alert('hi'); </script> </textarea>

displaying an empty text box and injecting the user’s JavaScript into the page.

Right now I am getting the contents of user-input and placing them into the textarea like so:

 $('#edit-textarea').html(($('#user-input').text()));

I’ve tried several variations of this to no avail.

Please see http://jsfiddle.net/vkRkt/8 for a test case.

EDIT:

See discussion below. The solution in my case was to use jquery.val() instead of jquery.html(). I skipped over val because I was under the impression that it was an alias for jquery.attr('value', new_value), which has its own injection issues (can be stopped with a “). val() is not the same as attr('value', val) and appears safe from injection attempts in my brief testing.

If anyone runs across this later and knows that val is actually not a safe way to do this, please answer and update us immediately.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:34:11+00:00Added an answer on May 26, 2026 at 12:34 am
    $('#edit-textarea').val(($('#user-input').text()));
    

    works fine for me. I also tried using .text() instead of .val() and it of course kept the escaping as well. Don’t use .html() if you don’t want the data to be HTML.

    jsFiddle Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to build an application that accepts user input data (such as name,
We need to accept a collection of key values from user input. Then a
I need to develop a template-like user control, which would accept any arbitrary content,
I have a function that need to accept two parameters- user and folder! I
I need to accept inputs from keyboard in php. When the user is done
I need to set up a server that will accept some text from my
I'll need to accept a string of 5 numbers from the user (only 5).
I need to fix this preg_match(/^[[0-9]{0,4}$/i, $input) , it also need accept negative values.
I need to accept values (username and password) from the user, when he clicks
I need a regular expression that accept numbers, spaces and hyphen with a maximum

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.