views:

45

answers:

2

I'm having an issue with jQuery's remove() method in IE. It's removing the element, but not entirely: it's leaving the last 2 closing tags.

I'm using ASP.Net Web Forms. In the page, we're using a 3rd party widget, which is a Javascript include. Part of the 3rd party widget is a search box and button inside of a form. (Everything in the div class="getquote" container below comes from the 3rd party widget).

Here is the page:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestFooBar.aspx.cs" Inherits="MyProject.TestFooBar" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div id="top">Some Stuff</div>
            <div class="getquote">
            <div class="box">
                <form style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px;
                padding-top: 0px" action="http://foo.com/?q=bar"
                method="post" target="_self">
                <input class="ticker" onclick="this.select()" value="Enter foo" maxlength="15"
                    type="text" name="fooInput" jquery123456789="42" />
                <input class="go" value="Get Foo" type="submit" name="Go" />
                </form>
            </div>
        </div>
        <div id="bottom">Some More Stuff</div>

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"&gt;&lt;/script&gt;
        <script type="text/javascript">
            $(function() {
                alert($("div").length)

                $('div.getquote').remove(); 

                alert($("div").length)
            });
        </script>
    </div>
    </form>
</body>
</html>

ASP.Net developers will immediately recognize a huge issue: you cannot have a form inside an ASP.Net web form, because web forms uses a single outer form element wrapped around the entire page.

So my solution is to use jQuery to remove the form and its functionality like so:

    $(function() { $('div.getquote').remove(); });

Unfortunately, remove() doesn't work correctly in IE. It leaves the following markup behind:

</form></DIV>

Can anyone explain why this is occuring and what a possible solution may be?

ANALYSIS UPDATE I still don't have a definitive solution, but believe the problem may be the improperly formed html. when you view the source through IE developer toolbar, here is the result.

<FORM id=form1 method=post name=form1 action=TestFooBar.aspx>
<DIV><INPUT id=__VIEWSTATE value=/wEPDwULLTE2MTY2ODcyMjlkZJK6qpmH4eDoZyoX9RueM4keR6Hd type=hidden name=__VIEWSTATE> </DIV>
<DIV>
<DIV id=top>Some Stuff</DIV></FORM>
<DIV id=bottom>Some More Stuff</DIV>
<SCRIPT src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"&gt;&lt;/SCRIPT&gt;

<SCRIPT type=text/javascript>
            $(function() {
                alert($("div").length)

                $('div.getquote').remove(); 

                alert($("div").length)
            });
        </SCRIPT>
</DIV></FORM>

So, while jQuery successfully removed most of the <div>, it left behind the closing </form> artifact.

A: 

As far as I can see with the given HTML the remove() method is working fine... It is removing the div with class getquote and all its children.

The form and divs left in the page are outside of the said element so it will not be removed by the given command $('div.getquote').remove();.

I think you may have to rethink about the contents of your HTML since there is a form element inside another form element, I think it is not a properly formatted html.

Arun P Johny
"As far as I can see with the given HTML the remove() method is working fine... It is removing the div with class getquote and all its children." -- but it's not removing the </form> piece that was stuck in it. If you use IE Dev Toolbar and click Alt-E, you'll see what I mean.
John
Umm, yes, I understand that you cannot have a form within a form -- in the question, I state this as the problem that I'm trying to solve. I have a 3rd party control rendering a form tag that I'm trying to remove.
John
A: 

I've come to the conclusion that what I'm trying to do is not possible in the browser layer. The problem with nesting a <form></form> element. IE will ignor the , but when it gets to the , it will close off the outer form, which throws of parsing.

I've then got several options:

1) Never, ever use ASP.Net Web Forms again. Instead go with ASP.NET MVC. OK, this is my dream, but not realistic for legacy apps.

2) Prefer XML feeds over JS includes when using web forms. Generally, this is probably better anyway, but managers love being told by a 3rd party that their widget can be dropped into a page in 5 minutes with only 1 line of code.

3) Don't call the 3rd party JS include in the browser, but instead, call it from a custom server method. Then, use Regex to strip the offending markup and pass what you need to the browser. Ugly hack that should only be done if 2) is not available.

John