I am trying to have a text box resize on click.
in the head:
<style>
.expand {
height: 1em;
width: 50%;
padding: 3px;
}
</style>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script>
$('textarea.expand').focus(function () {
$(this).animate({ height: "1000px" }, 500);
});
</script>
and body:
<form>
<textarea class="expand" rows="1" cols="10"></textarea>
</form>
it is not expanding for me.
The reason it doesn’t work is that you haven’t specified event-binding on
$(document).ready(), so the events are bound before the elements exist in the DOM. Try this:JS Fiddle demo.
It’s also worth noting that, in the more up-to-date browsers, you don’t necessarily require JavaScript for this:
JS Fiddle demo.