0

Possible Duplicate:
jQuery: select an element’s class and id at the same time?

when I have this ajax call to a function, I need to update a div with a class and a certain id:

        $(function() {
        $(".click-follow").click(function() {

            var user_id = "<?=$id?>";
            var id = $(this).attr('title');
            var dataString = 'user_id=' + user_id + '&id=' + id;

            $.ajax({
                type: "POST",
                url: "/ajax/follow.php",
                data: dataString,
                }).done(function( result ) {
                    myresult(result);
            });

            return false; 
        });
    });


function myresult(result) {
var result_lines = result.split("<splitter>");    
if (result_lines[0] == '1') {
    $('.click-follow').html(result_lines[1]).fadeIn(500);
    $('.follow').html(result_lines[2]).fadeIn(500); 
} else if (result_lines[0] == '2') { 
    $('.click-follow').html(result_lines[1]).fadeIn(500); 
    $('.follow').html(result_lines[2]).fadeIn(500); 

}
return true;   

}

So I want to use the var id in the function myresult, for example:

    $('.click-follow#' + id).html(result_lines[1]).fadeIn(500);

For example: I have 3 divs:

 <div class="click_follow" id="1"></div>
 <div class="click_follow" id="2"></div>
 <div class="click_follow" id="3"></div>

when I click div 1, I also want to update div 1. But I don't know how to use the var id in the function I call after the ajax call. The problem is that the amount of div's is not know... so it can be 20 div's or 2...

Community
  • 1
  • 1
Stefan
  • 155
  • 7
  • 18
  • `$('.click-follow#' + id)` looks good to me, what exactly are you having problems with? If `id` is empty or not accesible where you need it (which both seem to be the case in your code) this won't work. – Felix Kling Oct 14 '12 at 18:47
  • the problem is I want to pass the var id in the ajax call to the function I call after the ajax call.... – Stefan Oct 14 '12 at 19:01
  • Then do it. `myresult(id, result)` and adjust `myresult` accordingly. But instead of passing the ID and then selecting the element again, just pass the element itself. – Felix Kling Oct 14 '12 at 19:07

2 Answers2

0
$(".click-follow").click(function() {
  $("#fade").fadeIn()
});

Demo

bookcasey
  • 39,223
  • 13
  • 77
  • 94
0

I think what you actually want is a reference to the element that was clicked, which is pretty straightforward:

$(".click-follow").click(function() {
    var element = this;
     //...

     $.ajax({
         type: "POST",
         url: "/ajax/follow.php",
         data: dataString,
     }).done(function( result ) {
         myresult(element, result);
     });
});

function myresult(element, result) {
    // ...
    $(element).html(result_lines[1]).fadeIn(500);
}
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143