0

I have a AJAX call that finds any elements with the class videoReplace

  jQuery('.videoReplace').each(function(index) {
    jQuery.ajax({
        type: 'post',
        url: 'http://optinsmart.com/videos.php',
        cache: false,
        data:{
            'video': jQuery( this ).attr('video-source')
        },
        dataType: 'html',
        success:function(data){
            jQuery('#'+jQuery( this ).attr('id')).html(data);       
        },
        error: function(errorThrown){
            console.log(errorThrown);
        }
    });

  });

Here is my HTML

<h2>Tanya &amp; Matt Ice Cream</h2>
<div id="tanya" id-name="tanya" class="videoReplace" video-source="Opt-in Smart review by Tanya_Matt Ice cream"></div>

in theory this code should post to my video page and the video playing is sent back in html. Then i want to add the html to the div that im working with.

it tells me that jQuery('#'+jQuery( this ).attr('id')) is undefinded

Any suggestions?

JD Vangsness
  • 697
  • 3
  • 10
  • 27
  • 2
    Isn't `jQuery('#'+jQuery( this ).attr('id'))` the same as `jQuery( this )` ? – erik258 Mar 08 '14 at 20:23
  • 2
    exact duplicate of [$(this) inside of AJAX success not working](http://stackoverflow.com/questions/6394812/this-inside-of-ajax-success-not-working) – Felix Kling Mar 08 '14 at 20:26
  • its isnt doing anything even if i use `jQuery(this)` – JD Vangsness Mar 08 '14 at 20:27
  • 1
    Could you explain what `jQuery('#'+jQuery( this ).attr('id'))` does in your own words? And maybe you see then why this is unnecessarily convoluted :) – Felix Kling Mar 08 '14 at 20:27
  • it was a further try at getting the element when `jQuery(this)` wasnt working. Seemed bad but i was testing. i see that it was the exact same as `jQuery(this)`. – JD Vangsness Mar 09 '14 at 00:08

1 Answers1

2

Your problem is that this in the success callback is not the same as the this in the context of the each callback.

erik258
  • 14,701
  • 2
  • 25
  • 31