I’m not sure if it’s a bug in jQuery, FF/Chrome or IE or just expected behavior. Unfortunately I don’t know jQuery that well to tell if following script should give the same results under different browsers.
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
<div id="c">
<form name="login" action="https://my_domain.com/my_login_script.php" method="post">
<input type="text" name="email" value="sss" />
<input type="text" name="password" value="pass" />
<input type="submit" value="go" />
</form>
</div>
<script type="text/javascript">
$("#c").find('form[name=login]').submit(function() {
alert($(this).serialize());
$('#c').html("");
alert($(this).serialize());
alert($(this).html()); // empty string in IE, contents of <form> under FF
return false;
});
</script>
</body>
</html>
What it does:
- Assigns handler for submit event to form ‘login’ contained in div “#c”
- In the event handler form is serialized and displayed in alert box.
- Contents of div “#c” is changed to empty string.
- Form is again serialized and output string displayed in alert box.
- Contents of ‘this’ is displayed in alert box – it’s empty in IE and has some html in FF/Chrome
In FF/Chrome alert boxes show:
- email=sss&password=pass
- email=sss&password=pass // The same output as above, ‘this’ has not changed?
In IE9 however:
- email=sss&password=pass
- “” // Empty string
Is it a bug in jQuery?
I’d say it’s more of how the browser handles the
.innerHTMLproperty than a jQuery bug (as jQuery calls innerHTML, which the browser would use to construct/reconstruct a DOM tree). See this example: http://jsfiddle.net/bhKCu/1/ (if it were a jQuery bug, that should behave the same no matter which browser as it usesinnerHTMLinstead of$.html()right?).From jQuery.html():
From jQuery 1.7.1: