views:

1749

answers:

3

Dear all I have developed code with ajax and JQuery , I have Got Response from my.php, I have Tried to extract the Values of response ,Here the code

My.php

 ?php

    echo '<div id="title">My Title </div>';
    echo '<div id="message"> My message </div>';
    ?>

I Try to extract Title and Message SO My Code is Below

<script type="text/javascript" src="js/jquery-1.2.6.js"></script>

<script type="text/javascript">

  $(document).ready(function() {

        alert("OK");

        $.ajax({
          type:"POST",
          url: "my.php",
          cache:false ,
          success: function(data){
                  $("#response").html(data);

                  var $response=$(data);

                  var oneval = $response.find('#title').text();
                  var subval = $response.find('#message').text();
                  alert(oneval);

                }
             });
     });

</script>
</head>

<body>
<div id="response">
</div>

</body>
</html>

...

Problem is when I Tried To alert ,It Not working What Wrong with This Logic,Should i use anyother function To Extract Title and Message

A: 

Try using this instead:

var oneval = $("#response #title").text();
var subval = $("#response #message").text();
Michael Bray
Not Working Michael Bray ?
venkatachalam
Is the div actually filling with the content you are sending back? That is, can you see 'My Title' and 'My Message'?
Michael Bray
Michael, #response wont work as seen as there is not element with an id of response!
redsquare
#response is right in his HTML!
Michael Bray
not in the data returned hence it wont work....bah.....he is messing up his questions see http://stackoverflow.com/questions/400197/extracting-ajax-return-data-in-jquery#400232 also. Anyway there is no benefit or need to run two id selectors side by side....
redsquare
I suspect his return data is not as he says, he could really do with getting firebug installed and seeing what is actually happening
redsquare
It doesn't need to be in the returned data... notice that I'm using the root $ operator... not on $(response) as he indicates. The side-by-side selectors specifies that you want the 'title' id tag that is a descendant of the 'response' tag. There is a benefit - I use it all the time.
Michael Bray
I often get data that is html formatted, and then bind events (usually click) to elements that are returned in this manner. Important thing is to do it in the result handler.
Michael Bray
michael, trust me there is no benefit, it is slower than using just 1 id selector....in your case it has to filter to a subset first instead of using getElementById
redsquare
Although you are correct, the #response isn't necessary... He could probably just $('#title') and $('#message') in the handler.
Michael Bray
simple test case here to illustrate the non benefit (slowness)http://pastebin.me/495b37682a9e3
redsquare
But only in this simple case where there is only one parent element... I have many cases where I load similar content into different elements. Then you would have to use the parent selector.
Michael Bray
his original question asked how he can get those values without inserting into the dom, so i took the response and found the two elements and put them into vars. he seems to have changed track by inserting into the html first...
redsquare
Michael, id's are unique, you never need a parent selector when using an id selector. never!
redsquare
You're concerned about a 0.4ms difference??
Michael Bray
Re: unique id's... good point! I usually end up using class tags to identify these... it's 4am. :)
Michael Bray
just making a point, lots of 0.4 ms add up.....why make things ineffecient for the hell of it
redsquare
A: 

If the response is coming back as you wrote it then the code which i gave you will work. You really need to learn how to use a debugger (firebug) and put breakpoints into your code so you can use the console to test selectors and look at the values of variables etc. Also you should really have responded to the original question & answer that i gave rather than opening a whole new question especially since you have not even cross referenced your original.

redsquare
I done Your example with as you pointed out, When i try to alert the value, It simply throghs null message,Firebug says sucess.Still i not able to alert it how to do it ?
venkatachalam
Can you put breakpoints inside the success function and establish exactly what data is in the response.
redsquare
if not can you put this live and link the url so I can help
redsquare
http://pastebin.me/495b4572c4de1 is My Link
venkatachalam
This Link has Both my.php and Mycode http://pastebin.me/495b46c277910
venkatachalam
+1  A: 

ok go here http://jsbin.com/udige/ and you can see it working.

The key is using .filter not .find as the two divs are root elements in the response. My mistake. Basically do the following:

 success: function(data){
             $("#response").html(data);
                  var $response=$(data);
                  var oneval = $response.filter('#title').text();
                  var subval = $response.filter('#message').text();
                  alert(oneval);
                }
redsquare
Thanks for providing help!
venkatachalam