views:

110

answers:

2

Your help would be very much appreciated. I do not understand why the following jQuery code does not work:

function saveChanges(obj, n, id) {

  var t = "content to send to server";

  $.post("http://localhost:8080/Content.do?method=update",{
    value: t,
    key: id
  },function(result){
    alert(result); //WORKS
    alert("INNER"+$(obj).parent().parent().html());//WORKS
    $(obj).parent().parent().after('<div>dddddd</div>').remove();//FAILS ALL
    alert(id); //FAILS
    alert("stop"); //FAILS
  });

}

My conclusion so far is: inside this callback method function(result) I do have READ access to the outer object (here $(obj)) but I do NOT have WRITE access. As soon as I am doing a write the function fails from that point on to the rest of the function.

Is this conclusion correct? If someone knows about a good tutorial who explains this concept I would be greatful. (Object access with jQuery and their scopes...)

thank you very much

A: 

It sure has nothing to do with reading or writing "to" a variable. Something else must be going wrong on the first line you marked as "FAIL". You should check this by enabling Firebug in Firefox (if you don't have Firebug installed, drop everything and do that now) and running the sript again.

Philippe Leybaert
Hello Philippe,thanks for your answer. If my assumption is wrong than I know that i further have to debug this. I Spent a lot of time trying to find the error using "alert" statements for debugging but was not sucessfull... (I have Firebug installed...but haven't used it yet so its time to learn it now...)
A: 

There's something else wrong. In an isolated test case, I could not duplicate your error

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
<html lang="en">
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"&gt;&lt;/script&gt;
<script type="text/javascript">

//alert = console.log;

$(function(){

  function saveChanges(obj, n, id) {

    var t = "content to send to server";

    $.post("test.php",{
      value: t,
      key: id
    },function(result){
      alert(result); //WORKS
      alert("INNER"+$(obj).parent().parent().html());
      $(obj).parent().parent().after('<div>dddddd</div>').remove();
      alert(id);
      alert("stop");
    });

  }

  saveChanges( '#test', 1, 'test' );

});


</script>

</head>
<body>

<div>
    <div>
     <div id="test">
       just a test
     </div>
    </div>
</div>

</body>
</html>

And my test.php

<?php

echo json_encode( $_POST );

?>

I get four alerts

{"value":"content to send to server","key":"test"}


INNER
    <div>
     <div id="test">
       just a test
     </div>
    </div>


test


stop
Peter Bailey
thanks for your answer. Well I have a lot of otehr code inside this method and demonstrated by your answer, it seems there must be something elese wrong. I did a lot of debugging (but only using alerts) and whenever "writing" to the object the method was aborted so I came to this conclusion...
alerts are a very poor way to debug. Not only are they inadequate and just generally annoying, they can actually introduce error and skew that your script would not exhibit in the absence of those alerts. Switch to the console commands in Firebug (you can even see in the code above that, when I tested it, I remapped `alert()` to Firebug's `console.log()`, but commented it out before posting here).
Peter Bailey
Also, for the record concerning variables, there's no such thing as separate "read" and "write" access. Either the variable is available in the current scope or it isn't.
Peter Bailey