1

I'm working on a code generator and I am wondering if I can inject javascript into a text box USING javscript.

I do not want the injected javascript to be executed, and I'm worried that the quotes in the string may cause errors.

For example, I want to inject this script:

$('#click').function(){
  $('#thing').html('<a href="user.php">User</a>');
}

into this textbox:

<textarea rows="3" cols="20" id="textbox">
</textarea>

Using javascript(jQuery)'s .html().

How can I achieve that?

3 Answers 3

2

You can also use append function.

$('#click').function(){
$('#thing').append('<a href=\"user.php\">User</a>');
}
Sign up to request clarification or add additional context in comments.

Comments

1

Just set the value of the element:

$('#textbox').val("whatever, including JavaScript");

You'll have to make sure that the quote characters in the code you want to put into the <textarea> are quoted, but that's because you're expressing JavaScript source code in JavaScript and doesn't really have anything to do with the HTML element.

$('#textbox').val(
  "$('#click').function(){\n" +
  "  $('#thing').html('<a href=\"user.php\">User</a>');\n" +
  "}"
);

Comments

0

Did you try to use escape character sequences like \' or \" ?

Something like:

$('#textbox').html("$('#click').function(){
  $('#thing').html('<a href=\"user.php\">User</a>');
}");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.