Ticket #6458 (closed bug: duplicate)
jQuery html(text) parses the javascript code out of its level
| Reported by: | rtordable3 | Owned by: | rtordable3 |
|---|---|---|---|
| Priority: | undecided | Milestone: | 1.4.3 |
| Component: | core | Version: | 1.4.2 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description
$.html(text) parses the text and extracts the child javascripts elements from text and put them at the root level. It doesn't respects de parent/child relationship between the HTML tag elements and the child javascripts. When we iterate with $.each() the resulting elements parsed by $.html() the javascripts elements are in the root level and not in it child level.
Example: I have a parent div with two childs. The first one is a javascript element and the second one is another div with another javascript child.
Here you are the example code:
Example:
var html = $("<div><div id='container1'>" +
"<script type='text/javascript'>" +
"alert('hello world');" +
"</script>" +
"<span>hello world </span>" + "<div id='container1.1'>" +
"<script type='text/javascript'>" +
"alert('hello world 2');" +
"</script>" +
"</div>" +
"</div>" +
"<div id='container2'>" +
"<span>hello world 3</span>" +
"</div></div>");
html.each(function(i){
console.log($(this).html());
});
When we iterate the resulting jQuery object we obtain the following 3 elements:
<div id="container1"><span>hello world </span><div id="container1.1"></div></div><div id="container2"><span>hello world 3</span></div>
alert('hello world');
alert('hello world 2');
The expected result would be like jQuery 1.3.2 only 1 element and with html(), the javascript tags and its content is printed in their corresponding level.
Thanks.
Ramón
Change History
comment:2 follow-up: ↓ 4 Changed 3 years ago by rwaldron
- Owner set to rtordable3
- Priority set to undecided
- Status changed from new to pending
Please provide a reduced jsFiddle test case, thanks!
Additionally, test against the jQuery 0 GIT version to ensure the issue still exists.
comment:4 in reply to: ↑ 2 Changed 3 years ago by rtordable@…
Here you are the test case:
Tested with jQuery 0 GIT, 1.4.2 and 1.4.3 versions. It only works with jQuery 1.3.2. The expected result is one element with its javascripts childs included in their corresponding level.
Thanks.
Rtordable
Replying to rwaldron:
Please provide a reduced jsFiddle test case, thanks!
Additionally, test against the jQuery 0 GIT version to ensure the issue still exists.
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

With jQuery 1.3.2 the result is:
<div id="container1"><script type="text/javascript">alert('hello world');</script><span>hello world </span><div id="container1.1"><script type="text/javascript">alert('hello world 2');</script></div></div><div id="container2"><span>hello world 3</span></div>
The HTML code with the javascript.