bgrins / ExpandingTextareas

jQuery plugin for elegant expanding textareas
http://bgrins.github.com/ExpandingTextareas/
MIT License
260 stars 73 forks source link

Textarea doesn't expand when filled via jQuery load() #61

Closed OsakaWebbie closed 9 years ago

OsakaWebbie commented 9 years ago

Nifty little tool - thanks for putting it together in a jQuery sort of way after the initial alistapart version. I have one problem, though. I have a textarea that is sometimes filled with content in response to change in another field. In that case, it doesn't expand until some other action triggers the JS, like typing in the textarea. Here is my markup (a couple elements omitted for simplicity):

<form name="contactform" id="contactform" method="post" action="/individual.php?pid=5149#contacts" onSubmit="return ValidateContact()">
  <label class="label-n-input">Type: <select size="1" id="ctype" name="ctype">
    <option value="NULL">Select...</option>
    <option value="18">An option</option>
  </select></label>
  <textarea id="contactdesc" name="desc" class="expanding" wrap="virtual"></textarea>
  <input type="submit" value="Save Contact Entry" name="newcontact">
</form>

And my JS:

$(document).ready(function(){
  $("#ctype").change(function(){  //insert template text in Contact description when applicable ContactType is selected
    if (!$.trim($("#contactdesc").val())) {
      $("#contactdesc").load("ajax_request.php",{'req':'ContactTemplate','ctid':$("#ctype").val()});
    }
  });
});

Is there another event I should use to kick it into gear? Or call something of yours after the load() call?

domchristie commented 9 years ago

Is there another event I should use to kick it into gear?

Triggering a change event on the textarea, after load, should do the trick. Perhaps something like:

$(document).ready(function(){
  $("#ctype").change(function(){  //insert template text in Contact description when applicable ContactType is selected
    if (!$.trim($("#contactdesc").val())) {
      $("#contactdesc").load("ajax_request.php",{'req':'ContactTemplate','ctid':$("#ctype").val()}, function() {
        $(this).change();
      });
    }
  });
});
OsakaWebbie commented 9 years ago

Sorry I didn't respond sooner - I was buried deep in other fires to fight and didn't have a chance to do anything with my code. I just now tried it and it works - thanks.

So, there isn't an event that fires when AJAX fills a field? I would think that would cause problems for a lot of programmers.