Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
This feature that we are going to add to our Groundswell site is the ability to resize the form element for entering comments on our registration form and this is going to illustrate using one of the jQuery UI interactions, which is the Resizable interaction. So what I am going to do is click on the Register link here and you can see that there is a Comments field here and it's pretty sizeable. The user can type their comments. But what if we want to make this resizable so the user has more area to type in? So what we are going to do is go over to the code and implement the jQuery UI Resizable interaction to let the user resize that comments area.
So let's go to the code. So here we are in the folder for the example, and inside the lifestyle, there is a register file. I am going to open that up in my editor. Let's find that in the design. So that's down here in the code. So this is the text area right here, and this text area, we are going to make resizable and you can see it's wrapped inside of a div named comments section, and the text area itself has an id of comments.
So a couple of things we are going to do. First, we need to include the necessary jQuery library and the jQuery UI library at the top of the file. So we are going to go back at the top and we are going to do that. And we also include the CSS file. So I am just going to copy and paste this and the file is in the sunny sub-folder because that's the thing we downloaded. And it's called jquery-ui-1.7.2.custom.
All right, so now that we have included the script, we can assign the resizable behavior to our text area. So let's go ahead and do that and remember that's just a simple matter in jQuery of when the page loads, and I am going to declare my little page load function right here. So when the page loads, we are going to use jQuery to get a reference to the Comments section div and we are going to make that resizable.
So that's pretty much all we need to do to make it resizable. Let's see how that works out. So jump over to the browser. I am going to refresh. So you can see now that a little resize handle has been added here at the bottom of the Comments field, so we can resize. But you can see that there is a bit of a problem. We don't want the user to be able to resize this thing across the entire page. It will be nice if we can just constrain it somehow so that they can't make it too small or too big.
So let's set about solving that problem. So let's go back to the code. So what we need to do is specify that there is a maximum and minimum width for the field. So here in my function, I am going to declare a couple of variables. First, I am going to declare a maxWidth variable and what I am going to do is I am going to set that to be whatever the current width of the field is. That way it can't get any bigger than it currently is.
So I am going to use jQuery to retrieve a reference to the Comments section div, and I am just going to ask for its width. Remember from our CSS section that we can get the width of any element by doing this. Now in the resizable call, pass in some parameters to resizable, because remember it takes some parameters like what we want the min width and max width to be, and so on and so forth. So we are going to pass in an object and I am going to say that the maxWidth can't be any more than the maxw.
That's should fix the max problem. Let's go back to the browser and see. So I am going to refresh and yeah, sure enough, it can't get any wider than it used to be. That's its maximum, so that's good. I still have to fix the minimum problems though. So let's go back to the code. So we are going to do the same thing for the minimum width and minimum height. I am going to declare another variable named min width and I am going to set that to be equal to max width. So that way, the text area will only stay as wide as it currently is.
It won't get any more narrow and it won't get anymore wide. And finally, I am going to declare another variable and this one is going to be called min height. I am going to do the same kind of trick that I did with the width. I am going to copy that, I am going to paste this and now I am going to ask for whatever the current height is. Okay, so now, I know how wide it is and how high it is. I did just set the right parameters. There is maxWidth. I am going to do min height and that's minh and I am going to do min width and that will be minWidth.
Now I have set all the right parameters, all I need to do now is save, and we will go back to the browser, and now we will refresh. So now you can see that I can only resize the element up and down. I can't make it any wider, I can't make it anymore narrow, and I can only resize it down to the point where it was as high as it was to begin with. So I have pretty much used jQuery UI now to provide a feature to my users. They can now type in the field and now they can enter text and if they don't think it's big enough, well, they can just go ahead and resize that down.
I will just scroll the page down. So they can make it as nice and large as they want to. And just to make sure it works cross browser, let's try it on Firefox. So I am going to open this up in the Firefox browser and I'll scroll down and you can see, yup, there is the resize handle, and sure enough, yup, working just the same way. So that's pretty much all the features that we are going to use from the jQuery UI library.
However I highly encourage you to experiment. You can take the source code for the Groundswell site as you have it in your example files folder, if you have access to the example files. Feel free to play around with the jQuery UI settings and see what you can make happen.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99349 Viewers
56 Video lessons · 112609 Viewers
71 Video lessons · 81429 Viewers
131 Video lessons · 39091 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.