The difference between these two may be related to the following bugs: http://dev.jquery.com/ticket/3592 http://dev.jquery.com/ticket/2647
- Richard On Tue, Nov 18, 2008 at 1:21 PM, Richard D. Worth <[EMAIL PROTECTED]> wrote: > change > > var code = $("<div/>").html($("#content").val()); > > to > > var code = $("<div>" + $("#content").val() + "</div>"); > > - Richard > > > On Tue, Nov 18, 2008 at 1:14 PM, cc96ai <[EMAIL PROTECTED]> wrote: > >> >> I still get the popup window, if the textarea contains javascript >> popup >> it seems the following code is still executing. >> var code = $("<div/>").html($("#content").val()); >> >> >> <div id="raw"></div> >> <textarea cols="50" rows="5" id="content"> >> <a id="link" href="http://www.google.ca" target="_new" border="2">test >> link</a> >> <img src="images/test.jpg" border="1" vspace="2" alt="test"> >> <script> >> alert("popup"); >> </script> >> </textarea> >> >> On Nov 18, 7:49 am, Eric Martin <[EMAIL PROTECTED]> wrote: >> > I think this should do what you want: >> > >> > var code = $("<div/>").html($("#content").val()); >> > var links = $("a", code); >> > var images = $("img", code); >> > >> > links.each(function (i, lnk) { >> > $(lnk).removeAttr("target");}); >> > >> > images.each(function (i, img) { >> > $(img).removeAttr("border"); >> > >> > }); >> > >> > var clean = code.html(); >> > >> > On Nov 17, 6:17 pm, "Richard D. Worth" <[EMAIL PROTECTED]> wrote: >> > >> > > As shown earlier by Hector. Here's another example: >> > >> > > var foo = $("<div></div>"); // create an empty div element >> > > foo.html("<strong>Hello</strong>"); // set the html >> > > var html = foo.html(); // get the html >> > >> > > It's just in memory, all disconnected from the DOM, as it was never >> appended >> > > to the body or any other element in the document. You can also do fun >> things >> > > like >> > >> > > var strong = $("strong", foo); // find strong elements within the >> context of >> > > the (in this case) disconnected DOM Element foo, instead of the >> default >> > > context, the current document >> > >> > > - Richard >> > >> > > On Mon, Nov 17, 2008 at 6:27 PM,cc96ai<[EMAIL PROTECTED]> >> wrote: >> > >> > > > if div foo is outside the DOM, how could we use jQuery to assign the >> > > > HTML into it ? and retrieve it back ? >> > >> > > > On Nov 17, 1:51 pm, "Hector Virgen" <[EMAIL PROTECTED]> wrote: >> > > > > The div will only be part of the body if you append it to the >> body. >> > > > > var div = document.createElement('div'); // not part of the dom >> yet >> > > > > div.setAttribute('id', 'foo'); // set an id, but it's still not >> part of >> > > > the >> > > > > dom >> > > > > document.body.appendChild(div); // now the div is part of the dom >> > >> > > > > But I'm not sure how <script> tags are handled prior to dom >> insertion. >> > >> > > > > -Hector >> > >> > > > > On Mon, Nov 17, 2008 at 1:45 PM,cc96ai<[EMAIL PROTECTED]> >> > > > wrote: >> > >> > > > > > how could I create the DIV outside the DOM ? >> > > > > > if I create a <div> inside the <body>, it is part of the DOM, >> isn't >> > > > > > it ? >> > >> > > > > > On Nov 17, 12:32 pm, "Hector Virgen" <[EMAIL PROTECTED]> >> wrote: >> > > > > > > Will javascript be executed by jQuery#html() if the container >> is not >> > > > part >> > > > > > of >> > > > > > > the dom? >> > > > > > > -Hector >> > >> > > > > > > On Mon, Nov 17, 2008 at 12:21 PM,cc96ai< >> [EMAIL PROTECTED]> >> > > > > > wrote: >> > >> > > > > > > > That is my original design, however if the textarea contains >> the >> > > > > > > > javascript, it will execute the Javascript in div as well. >> > >> > > > > > > > in the following example, the popup will come out when we >> try to >> > > > > > > > populate the textarea content into div. >> > > > > > > > that's why I am looking other way to parse the content. >> > > > > > > > e.g. >> > > > > > > > <textarea cols="50" rows="5" id="content"> >> > > > > > > > <a id="link" href="http://www.google.ca" target="_new" >> > > > border="2">test >> > > > > > > > link</a> >> > > > > > > > <img src="images/test.jpg" border="1" vspace="2" alt="test"> >> > > > > > > > <script> >> > > > > > > > alert("popup"); >> > > > > > > > </script> >> > > > > > > > </textarea> >> > >> > > > > > > > Thanks, >> > >> > > > > > > > On Nov 17, 12:01 pm, "Hector Virgen" <[EMAIL PROTECTED]> >> wrote: >> > > > > > > > > You can try assigning the value of the textarea to a >> hidden div. >> > > > Then >> > > > > > > > you'll >> > > > > > > > > have access to jQuery's functions on the HTML: >> > > > > > > > > var html = $('#content').val(); >> > > > > > > > > var div = $('<div />').css({display: 'none'}); >> > > > > > > > > div.html(html); >> > > > > > > > > div.find('a[target]').removeAttr('target'); >> > >> > > > > > > > > Then replace the textarea with the html contents of the >> div: >> > >> > > > > > > > > var filtered = div.html(); >> > > > > > > > > $(#content).val(filtered); >> > >> > > > > > > > > -Hector >> > >> > > > > > > > > On Mon, Nov 17, 2008 at 11:22 AM,cc96ai< >> > > > [EMAIL PROTECTED]> >> > > > > > > > wrote: >> > >> > > > > > > > > > If we do the replace , it will replace all the tag's >> border & >> > > > > > target. >> > >> > > > > > > > > > is there anyway it can replace on tag level ? >> > >> > > > > > > > > > On Nov 17, 10:34 am, Eric Martin <[EMAIL PROTECTED]> >> wrote: >> > > > > > > > > > > If you just want the "string" value, how about: >> > >> > > > > > > > > > > var content = $("#content").val(); >> > > > > > > > > > > content = content.replace(/ >> > > > target=(\'|\")_(new|blank)(\'|\")/, >> > > > > > ""); >> > > > > > > > > > > content = content.replace(/ border=(\'|\")\d+(\'|\")/, >> ""); >> > >> > > > > > > > > > > -Eric >> > >> > > > > > > > > > > On Nov 17, 10:18 am,cc96ai<[EMAIL PROTECTED]> >> > > > wrote: >> > >> > > > > > > > > > > > I have a text area in the following >> > > > > > > > > > > > <textarea cols="50" rows="5" id="content"> >> > > > > > > > > > > > <a id="link" href="http://www.google.ca" >> > > > target="_new">test >> > > > > > > > link</a> >> > > > > > > > > > > > <img src="images/test.jpg" border="1" vspace="2" >> > > > alt="test"> >> > > > > > > > > > > > </textarea> >> > >> > > > > > > > > > > > and I would like to strip out the hyperlink target, >> and >> > > > image >> > > > > > > > border, >> > > > > > > > > > > > and get the string of the HTML, >> > > > > > > > > > > > any idea on it? >> > >> > > > > > > > > > > > I try the following, but it only remove the >> hyperlink in >> > > > the >> > > > > > page, >> > > > > > > > not >> > > > > > > > > > > > the hyperlink in textarea. >> > > > > > > > > > > > $("a").removeAttr("target"); >> > >> > > > > > > > > > > > Can I load the textarea 's content in jquery, then >> do the >> > > > > > select/ >> > > > > > > > > > > > remove the attribute and return as string? >> > >> > > > > > > > > > > > Thanks >> > >