Jump to content

A little help with jquery and divs


takn25

Recommended Posts

Hi,  Ill post the code first.

 


$(function()

{

$('.follow').click(function(){

var name=$(this).attr('name');
var info= 'test=' + name;

$.ajax({  
      type: "GET",  
      url: "yes.php",  
      data: info,  
      success: function(data){  
               $('.test_results').html(data); 

          }  
        });  
  })

})





<body>
<button name='yabadaba' class="follow" > </button>
<div class='test_results'> </div>

<button name='whatsup' class="follow" > </button>
<div class='test_results'> </div>



</body>



 

Ok as you can see there are two buttons and two div classes which are the same except the name difference between the buttons. What I want to achieve when the result comes from PHP I only target the div class below the button and not show the results in all the divs with the class test_result. Any help is appreciated thanks.

 

 

Link to comment
https://forums.phpfreaks.com/topic/235275-a-little-help-with-jquery-and-divs/
Share on other sites

  • 2 weeks later...

Use jQuery's next() function.

 

Get the immediately following sibling of each element in the set of matched elements. If a selector is provided, it retrieves the next sibling only if it matches that selector.

 

$(function() {

    $('.follow').click(function() {
    
        var name = $(this).attr('name');
        var info = 'test=' + name;
        var $this = $(this);

        $.ajax({  
            type: "GET",  
            url: "yes.php",  
            data: info,  
            success: function(data) {  
                
                $this.next('.test_results').html(data);  
            }  
        });  
    });    
});

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.