Skip to main content

Posts

Showing posts from January, 2013

How To Get Height Of An Element In jQuery

There is are three methods in JQuery that deals with the height of a element. .height() : it measure the height of the element/container excluding applied padding, margin and border-width. .innerHeight() : it measure the height of the element/container excluding applied margin. It includes applied padding but not border-width of the element. .outerHeight() : It measures the height of the element/container including all applied padding + border width but not margin. If you want to include margin too, you should use .outerHeight(true) Any of the method will provide 'pixel' value of the height of container. We just have to select the method which that suits our requirement. first of all apply style="height:auto;" in element, so it can get the height as per content in it. then use these methods as below: Loading .... The point to be noted here is, I'm using ('#element-id').ready() and not DOM ready. Sometimes when we use it on DOM ready...

Resize Textarea on 'Enter' pressed

1. Add a textarea to a simple HTML template 2. Now include jQuery CDN or downloaded jquery to your page in head section 3. Add a key press event in document scope. 4. Check if the pressed key is 'Enter'. Also check if control is textarea. (I added event only for Text Area, so no need to check here) 5. Add another event on blur to reduce its size if white space added by user. 6. Get the height of existing control and add some more height. Check-it-out with below snippet. :)