2

I am developing a small editor to add widgets (HTML, CSS and Javascript) to a site.

  • The user types into a field the widget code.
  • When validating, the code of the widget is added to the page.
  • The user can edit the code (which is retrieved from the DOM).

Except that I have a problem to retrieve the javascript code. Indeed, it is well executed, but disappears from the DOM. There is something that escapes me!

I simplified my problem here: HTML code:

<textarea id="code">
    &lt;script type=&quot;text/javascript&quot;&gt;alert(&quot;code&quot;);&lt;/script&gt;
</textarea>
<div id="target"></div>

Javascript code:

$("#target").html($("#code").val());
alert($("#target").html());

Try it out : http://jsfiddle.net/8uhB8/3/

This example insert javascript code (a simple javascript alert) and tries to retrieve it.

Do you have any idea?

Thanks!

2
  • This is old, but did you find a solution? I have the same problem. Thanks Commented Nov 7, 2014 at 18:16
  • I found the issue on my case, maybe not the same as yours: the js code is executed when a form submit button is clicked, so the page is quickly refreshing after the js code runs. Commented Nov 7, 2014 at 18:22

2 Answers 2

2
$("#target").html($("#code").html());
alert($("#target").html());

Fiddle.

Because you are using HTML characters you need to use .html() in this case.

Sign up to request clarification or add additional context in comments.

1 Comment

If you compare your "fiddle" with mine, you will see that there is an important difference: the javascript code is written on the page and not executed. This is not the expected result.
2

That's how .html() works. It assigns .innerHTML with all the structural parts of the HTML. But assigning innerHTML doesn't execute scripts. So jQuery pulls all the scripts out of the HTML, and puts them in a <script> node to execute them. This node is only temporary, and is removed by the time .html() completes. But the scripts are still executed.

6 Comments

Interesting answer! Do you think it is possible to achieve the expected result by changing my code? Do you have any ideas?
Don't try to retrieve the code from the DOM. Put the code in the textarea using .text(), put it in the DOM using .html().
This is a good solution but it does not apply to my problem. Indeed, in my online web page editor, the textarea is created on the fly to edit the code and then destroyed. There may be multiple widgets on the same page. I'm beginning to think that what I want to do is impossible?
It sounds like you're trying to do something like jsfiddle. Why can't you do it similar to the way it works?
To clarify, I have an online web page editor that takes as input a code (HTML, CSS, Javascript, etc.), this code is processed to be manipulated and edited by the user. In this code, I have title blocks (H1, H2, etc..), text blocks, widgets, etc. Each is editable through a window. I want to allow the user to modify the code of a widget. That it is executed after modification. And I found this code in the html.
|

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.