At the moment, I have this DIV with a registration form centered over the page. The contents of the DIV come from an ascx-page. This is done nicely.
Now, if the user tries to fill in a name that’s not unique, an error message is added by some jQuery next to the username field. This breaks the layout of the DIV, since the contents now are wider than they used to be.
So I’ve googled my way into this, but I can’t find a solution for this.
Can anyone help me find a nice way to do this (pseudo HTML/js):
<div id="myCenteredDiv" onChangeContents="resizeToNewSize()">
<!-- div contents -->
</div>
<script type="text/javascript">
function resizeToNewSize() {
$("#myCenteredDiv").animateToSize($("#myCenteredDiv").contentWidth,
$("#myCenteredDiv").contentHeight);
}
</script>
I’m looking for the “onChangeContents” method (and/or event) and the “div.contentWidth” property.
Thanks a lot for helping me out!
update: trying to explain the problem more clearly
Let’s say I have this DIV:
<div id="myDiv">
<form ...>
Enter your name: <input id="txtYourName" type="text" name="YourName" value="" />
<span id="errorYourName"></span>
<input type="submit" ... />
</form>
</div>
And let’s say I have this snippet of jQuery:
$(document).ready(function() {
$("#txtYourName").live("blur", function() {
if (validateInput($("#txtYourName").val())) {
$("#errorYourName").html("");
// entry 1
} else {
// entry 2
$("#errorYourName").html("This name is not valid.");
}
});
});
…where validateInput(value) returns true for a valid value.
Well now. The SimpleModal plugin takes the div and places it centered on the page, with a certain width and height it somehow reads off of the contents of the div. So the div isn’t wider than the input box, since the span is empty at the moment.
When the input box loses focus, an error message is put into the span. This then breaks the layout of the div.
I could put code into entry 1 that resizes the div back with an animation to some size when the error message has been cleared, and code into entry 2 that resizes the div to a bigger size so the error message will fit.
But what I’d like best is a way to tell if the contents within the div have changed, and fit the div accordingly, animated and automatically.
Any ideas? Thanks again.
I’ve never used SimpleModal, but from the examples on their site it looks like you can set the container CSS. If you want the height to adjust, try setting the height to
autoAlthough the example doesn’t have it, I’d think you need to add
pxafter the height and width in quotes (e.g."450px").Ok here is another idea. Maybe this is too much, but add a hidden input field:
then attach a change event which is triggered at the same time you update the error message.
This is untested and it may be going overboard, but I don’t see an update function in SimpleModal.
Update : Sorry I figured out that blur isn’t supported with live event. So I did some further testing and came up with a working demo. I posted it in this pastebin (ignore the included simpleModal code at the bottom). Here is the essential code
CSS
HTML
Script