Tag Archives: manipulation

Automatically expand textarea to fit content using jQuery without autogrow or autosize plugins

I’m sure there is a more efficient way to do this, but it suited my needs.

<script>
$(function() {
    $('textarea').keyup(function(){
        $(this).each(function(index, element) {
            $(element).height(element.scrollHeight);
        });
    }).keyup(); //trigger event to initialize pre-filled textareas
});
</script>

Enjoy 🙂

Calculate the total height of child elements of a clipped (overflow: hidden) parent tag

Ever have an element with clipped content and wanted to know the total height of the elements contained therein? I have a client who wants the height of the main content to be clipped to a preset height (don’t ask me why). Which means I need to dynamically add a “read more” link to expand the container and show all the content.

Well, here’s how you calculate the combined height of the children with jQuery… Continue reading

Remove www prefix from domain using PHP

I see this question a lot and usually it comes down to getting the current host name for use with cookies. Lets say I want to make a domain-wide cookie, that does not include the standard www prefix such as www.robertmullaney.com. In order for the cookie to work with or without the www prefix, it needs to become ‘.robertmullaney.com’.

preg_replace('/^www\./i', '.', $_SERVER['HTTP_HOST'])

Now, some of you may be looking for a way to determine the top-level domain for something like this.subdomain.robertmullaney.com. That is not an easy solution (since many TLDs have multiple suffixes like .co.uk). If that’s what you’re looking for, check back later and see if I have come up with a good solution… that one’s tricky.