JQuery Ajax autosave for textareas

Go To StackoverFlow.com

0

I've been able to get my autosave to work fine for input boxes and select boxes. I'm trying to use the same process for textareas but it's not working for me. I'm not sure if the same process can be used. I also have tinymce as an htmleditor for my textareas so I'm not sure if that's causing an issue.

Here's an example of my code. I'm doing this within a classic ASP page.

<textarea id="Com<%=QuesID%>" row= "1" cols= "120" name="Com<%=QuesID%>" QuesID=<%=QuesID%> wrap tabindex="21" rows="10" class="formTxt"><%=TempTxt%></textarea>

Then at the bottom of the page:

<script>
$(document).ready(function(){
        $('select').live('change',function () {
                 var itemValue = escape($(this).val());
                var itemName = $(this).attr('name');
                var QuesID = $(this).attr('QuesID');
                var typeID = "select";
                //alert(statusVal);
                $.ajax({
                         type: "POST",
                         url: "PAFormAJAX.asp",
                         data: 'itemValue=' + itemValue + '&itemName=' + itemName + '&QuesID=' + QuesID + '&typeID=' + typeID,
                         success: function(msg) {
                             $('#autosavenotify').text(msg);
                         }
              })
          });

          $('input').live('change',function () {
                 var itemValue = escape($(this).val());
                var itemName = $(this).attr('name');
                var QuesID = $(this).attr('QuesID');
                var typeID = "input";
                //alert(statusVal);
                $.ajax({
                         type: "POST",
                         url: "PAFormAJAX.asp",
                         data: 'itemValue=' + itemValue + '&itemName=' + itemName + '&QuesID=' + QuesID + '&typeID=' + typeID,
                         success: function(msg) {
                             $('#autosavenotify').text(msg);
                         }
              })
          });

          $('textarea').live('change',function () {
                var itemValue = escape($(this).val());
                var itemName = $(this).attr('name');
                var QuesID = $(this).attr('QuesID');
                var typeID = "textarea";
                //alert(statusVal);
                $.ajax({
                         type: "POST",
                         url: "PAFormAJAX.asp",
                         data: 'itemValue=' + itemValue + '&itemName=' + itemName + '&QuesID=' + QuesID + '&typeID=' + typeID,
                         success: function(msg) {
                             $('#autosavenotify').text(msg);
                         }
              })
          });


});
</script>

The saving was working fine for inputs and select boxes, so I'll leave out the PAFormAjax.asp code. Am I able to autosave with textareas like this? If not, do you have any tips for what I need to change?

Thanks for any help!

2012-04-04 20:04
by Cineno28
Where is the error occuring/how do you know it's not working. Does it not show up, does the ajax call 500? Does the ajax call even get out? Where is it not working - dennmat 2012-04-04 20:08


1

Try using

encodeURIComponent($(this).text());

for textareas

2012-04-04 20:09
by jornare
so I would use this in place of the "var itemValue = escape($(this).val());" that I have now? I'm just trying to make sure I understand where you mean I should substitute this - Cineno28 2012-04-04 21:27
Yes. TextArea does not have a value attribute, but stores its content inside the html as innerText or .text() in jQuery. the encodeURIComponent() is a more approperiate method for escaping - jornare 2012-04-04 21:30
Ok great. I just tried it and now I'm getting sucess messages from the Ajax page so it's finally sending it properly. My textareas had the tinymce html editor on it. I had to deactivate that for it to work. Do you know how I can accomplish this and keep the editor on my textareas - Cineno28 2012-04-04 21:39
I don't think tinymce uses textareas at all, but a div with contenteditable=true. You'll then have to check if tinymce has a javascript save function or value property from which you can recieve the content - jornare 2012-04-04 21:49
Ok thanks, I'll look into that. I really appreciated your help with this - Cineno28 2012-04-04 22:02


1

This is only valid for very old jQuery versions, see the end of this post.

.val() is only for input elements, textareas use .text().

This is because jQuery just check the value attribute of the element, which isn't used for textareas:

<textarea value="Some text here"></textarea>

That won't work, you have to use it like this instead:

<textarea>Some text here</textarea>

In very old versions of jQuery, val was simply defined as this, which only would work for values (extract from jQuery 1.1.1):

val: function( val ) {
        return val == undefined ?
            ( this.length ? this[0].value : null ) :
            this.attr( "value", val );
    }
2012-04-04 20:23
by Henrik Karlsson
$('textarea').val(); works fine. Check this out: http://jsfiddle.net/f5tLb - Conrad Warhol 2012-12-07 18:03


0

Is there a reason you don't opt to serialize the whole form? and just handle the processing page appropriately? http://api.jquery.com/serialize/

$(document).on('change', function () {
  $.ajax({
    type: "POST",
    url: "PAFormAJAX.asp",
    data: $('form').serialize(),
    success: function(msg) {
        $('#autosavenotify').text(msg);
    }
  });
}, 'select, input, textarea');

I'm making some assumptions in the pasted code - replace what you need and try to use a better selector than $(document) if you can - I assume you're using .live because you're working with a DOM that has been modified in some way.

2012-04-04 20:18
by mikevoermans
Thanks for the suggestion, but I'm not using serialize because the form elements load dynamically and the form has the ability to be very large. I want to pass values only when a change is made to an element so the save script only saves individual changes, and not the entire form because that would make the page very slow - Cineno28 2012-04-04 21:25
Ads