-1

I have a div that contains some html and a lot of Javascript

<div id="mydiv">
<p>Hello Philippines</p> my first time in Philippines is nice
<script type="text/javascript">alert("how was it became nice?");</script>
well I experienced a lot of things
<script type="text/javascript">alert("and how about your Japan's trip?");</script>
well its more nicer ^^ but both countries are good! hahah
</div>

I want to put the non-Javascript code and Javascript code in two separate variables:

var html = $("#mydiv").html();

My problem here is that my javascript is executing, which makes me stop to create the code I want which is the storing of javascript and non-javascript to two different variables.

  1. how can I stop the javascript codes from executing when they are get inside the div?
  2. how can I store the javascript and non-javascript code into two different variables safely?

NOTE: I need the stored javascript for later execution

8
  • 1
    Put your javascript code inside the funciton. They won't be executed until they have been called. Commented Jul 7, 2012 at 9:10
  • 1
    1) use text instead of html, 2) you probably need some regex to separate them Commented Jul 7, 2012 at 9:12
  • when are you going to run the javascript? your script will run on page load that what i see. Commented Jul 7, 2012 at 9:12
  • well i was able to think about it but the problem here is the javascript is end-user defined code and I can't let them type the function every time Commented Jul 7, 2012 at 9:12
  • well there is an user-event(a button click) that the javascript code will be executed as the end-user's desire Commented Jul 7, 2012 at 9:15

1 Answer 1

2

you can use code tag instead of script:

html:

<div id="mydiv">
<p>Hello Philippines</p> my first time in Philippines is nice</p>
<code>alert("how was it became nice?");</code>
well i experienced a lot of things
<code>alert("and how about your Japan's trip?");</code>
<p>well its more nicer ^^ but both countries are good! hahah
</p>
</div>

js:

var html = $('#mydiv *').not('code').html()
var code = $('#mydiv code').text()

http://jsfiddle.net/dMDy5/3/

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

1 Comment

hehe you just gave me the answer! well thank you ^^ but is there any workaround to handle with script tags instead of code tags?

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.