jQuery/AJAX - Can't get the correct selector
I'm looking for a little assistance, but mostly an explanation so I can avoid this issue in the future.
I have a PHP script that is called using AJAX. The output of that script fills a div with an ID of 'schedule_wrapper_all' and is as follows:
- <div id="schedule_wrapper_all">
- =====================GENERATED CODE======================
- <div class="appointments">
- <div class="appt" value="2">
- <p class="c_name">William Davis</p>
- <div class="c_info" style="display: block;">
- <p class="c_street">230 N State Rd</p>
- <p class="c_city">Davison</p>
- <p class="c_time">11:00:00</p>
- <p class="c_phone">Phone 1: </p>
- <p class="c_phone_alt">Phone 2: </p>
- </div>
- <div class="c_functions" style="display: block;">
- <p><button type="button" class="view_notes">View Notes</button></p>
- <div class="d_view_notes" style="display: none;"></div>
- <p><button type="button" class="add_note">Add Note</button></p>
- <p><button type="button" class="reschedule">Reschedule</button></p>
- <p><button type="button" class="reassign">Reassign</button></p>
- </div>
- </div>
- </div>
- ==================END GENERATED CODE====================
- </div>
Here's the jQuery:
- $('#schedule_wrapper_all').on('click', '.appointments .appt .view_notes', function(e) {
- e.stopImmediatePropagation();
- var leadID = $(this).closest('.appt').attr('value');
- $.ajax({
- method: 'post',
- url: 'scripts/get_notes.php',
- data: {
- 'leadID': leadID
- },
- success: function(data) {
- //=============PROBLEMATIC AREA=================
- $('.d_view_notes').html(data).slideToggle();
- //==============================================
- }
- });//end ajax - Fill Notes
- });
The script works, except the data filled by the AJAX call fills to all divs with the class 'd_view_notes', and there are multiple on the screen. What I want to happen is that when the button is clicked, it only fills the 'd_view_notes' contained within the selected 'appt' div.
After hours of trying, the closest I have come to getting the right selector is:
$(this).closest('.c_functions').find('.d_view_notes').html(data)
but is is not working. I would not only appreciate the right selector, but a quick explanation of WHY I need to choose that selector. Thanks so much for any help!