0

I have a javascript files that is suppose to interact with the response of my ajax request but apparently the javascript can't read the response from the ajax request.

What I'm asking is, how can I make my jquery plugin read the class in the response of my ajax request?

This is how my script looks without ajax:

 <body>
 <div id="main_content">
 <!--this is where the ajax is returning the info-->
 </div>
 </body>
 <body>
 <div id="main_event_saff">
 <!--this is an example of the ajax returns-->
 <table>
 <tbody>
<tr>
<td class="header">Header</td>
 </tr>
 <tr>
<td class="data">Data</td>
</tr>
 </tbody>
 </table>
 </div>
 </body>

Ajax file:

 $(document).ready( function rankings(callback){
 $.ajax({
 url: 'ajax/rankings.php',
 type: 'GET',
 success: function(response){
 $('#main_event_saff').html(response);
 }
 });
});

Here's the plugin that I want to read the response of the ajax request:

 $(document).ready(function () {
 $(".data").hide();
$(".header").click(function () {
 $(this).next(".data").slideToggle(200);
});
});

This doesn't work because it can't read the ajax request

just like the page source it only see's

jogesh_pi
9,7745 gold badges40 silver badges65 bronze badges
asked Mar 28, 2014 at 4:42
7
  • 2
    "I make my jquery plugin" why not use $.ajax() from jquery library that work across cross browser? Commented Mar 28, 2014 at 4:44
  • Agreed, your already using jQuery, so I would use $.ajax() as well... otherwise you are just making it difficult Commented Mar 28, 2014 at 4:46
  • Could main_event_saff be incorrect spelling? Could it actually be main_event_staff or main_content ? If the ID is misspelled the update will fail. Commented Mar 28, 2014 at 4:59
  • @jogesh_pi I'm actually using jquer now Commented Mar 28, 2014 at 5:04
  • @mattchambers you are trying to sliding the <td class="data"> right? Commented Mar 28, 2014 at 5:17

4 Answers 4

2

As you said you are using jQuery, why not this

 $(document).ready( function rankings(callback){
 $.ajax({
 url : 'ajax/rankings.php',
 type : 'GET',
 data : 'yourData',
 success : function(response){
 $('#main_event_saff').html(response); 
 }
 });
 });

this will make your work easy

answered Mar 28, 2014 at 4:50
Sign up to request clarification or add additional context in comments.

Comments

2

You can use simply $.load() a jQuery ajax request:

$( "#main_event_saff" ).load( "ajax/rankings.php" );

You might want to do something more on getting the response :

$( "#main_event_saff" ).load( "ajax/rankings.php", function() {
 alert( "Load was performed." );
});
answered Mar 28, 2014 at 5:01

Comments

1

Click event on dynamic element should work like this.

$(document).ready(function () {
 $("#main_event_saff").on('click', '.header',function () {
 $(this).next(".data").slideToggle(200);
 });
});

for more information take a look on the .on()

answered Mar 28, 2014 at 5:28

Comments

0

You haven't used

id="main_event_staff"

in your HTML so how would you eventually read/write to its inner HTML.

answered Mar 28, 2014 at 4:54

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.