3 Replies - 1237 Views - Last Post: 05 December 2011 - 04:19 PM

#1 Btu   User is offline

  • D.I.C Regular

Reputation: 36
  • View blog
  • Posts: 250
  • Joined: 16-May 11

.each() pulling old data from textarea ?

Posted 05 December 2011 - 03:15 PM

Hi guys,

Here's my goal: Loop through Textareas that are children of a div, retrieve their texts and .push into an array.

Problem: After I make a change to the text in the textarea and click "save", it loops through the textareas and retrieves the data that was in there to begin with. Not the new data I enetered.

Can anyone explain this behaviour?

here is my code:

$("#update_info").click(function () {

    var arr_i = new Array();

    $("#class_assignments i").each(function (index) {
        arr_i.push($(this).text());
    });
    $("#class_assignments textarea").each(function (index) {
        alert($(this).text()); // This alert is showing me the old data. I want to change & save this to DB
        $.post("update_info.php", {
            u_asmt: arr_i[index],
            u_info: $(this).text()
        }, function (output) {
            alert(output);
        });
    });
});



Anyone have any suggestions?

Is This A Good Question/Topic? 0
  • +

Replies To: .each() pulling old data from textarea ?

#2 codeprada   User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: .each() pulling old data from textarea ?

Posted 05 December 2011 - 03:46 PM

How do you make the change, through code or manually?
Was This Post Helpful? 0
  • +
  • -

#3 Btu   User is offline

  • D.I.C Regular

Reputation: 36
  • View blog
  • Posts: 250
  • Joined: 16-May 11

Re: .each() pulling old data from textarea ?

Posted 05 December 2011 - 03:59 PM

Initially, you choose a class from a dropdown box which gets a list of classes from a database:


 $("#classes").change(function () {
			 if ($('option:selected', '#classes').index() != 0){
				$("#change_teacher").removeAttr("disabled");
				$("#delete").removeAttr("disabled");
				$.post("getclasses.php", {
                    q: $("#classes").val()}, function (output) {
						$("#class_assignments").html("");
						$("#class_assignments").html(output);
					    $("<input type=\"button\" value=\"Update Info\" />").attr("id", "update_info").appendTo("#class_assignments");
						$("#update_info").click(function(){	
							var arr_i = new Array();	
							$("#class_assignments i").each(function(index){
							arr_i.push($(this).text());
							});
							$("#class_assignments textarea").each(function(index){
							alert($(this).text());
						
							$.post("update_info.php", {
								u_asmt: arr_i[index],
								u_info: $(this).text() },
							 function (output) {
								alert(output);
							});
							
							});
						});

					});	
				}
			});
			  
		 



When I click the button, it loops through the textareas and gets the old data (what was in orginally), even though i typed new data in the textarea, when i loop through it only shows the old data. Not the new.

This post has been edited by Btu: 05 December 2011 - 04:08 PM

Was This Post Helpful? 0
  • +
  • -

#4 Btu   User is offline

  • D.I.C Regular

Reputation: 36
  • View blog
  • Posts: 250
  • Joined: 16-May 11

Re: .each() pulling old data from textarea ?

Posted 05 December 2011 - 04:19 PM

Well I fixed it myself. I appreciate you taking the time to look...

Here is the solution..

my click handler had to be in the same context at the change event handler, or it won't recognize it. (duh!)

And I was using .text() ... and .text() picks up the initial text property of the textarea, when I change to .val() it picked up the new entered value.

Thanks guys
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1