Skip Navigation

[Resolved] Javascript to hide/show div in View loop item targeting first result only

This thread is resolved. Here is a description of the problem and solution.

Problem:
I have buttons next to loop items in my View. I want to hide/show div in View loop item targeting first result only, so that user can toggle the content while pressing the button.

Solution:
Solution here with the JS code:
https://toolset.com/forums/topic/javascript-to-hideshow-div-in-view-loop-item-targeting-first-result-only/#post-578599

This support ticket is created 7 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 4 replies, has 2 voices.

Last updated by jonB-5 7 years, 1 month ago.

Assisted by: Noman.

Author
Posts
#578537
Screen Shot 2017-10-13 at 14.22.36.png
Screen Shot 2017-10-13 at 14.21.38.png
Screen Shot 2017-10-13 at 14.10.02.png

I have a form placed within a View loop item that I have set to hide/show using Javascript. As per the attached screenshot, when a user clicks "Add a Booking", the booking form appears within the loop item.

The problem is, if a user clicks "add a booking" in the second loop item, the form appears in the first loop item, but not in the loop item where the booking button was clicked.

Can you advise on whether or not there is a solution to this, such that the item will show in the loop item where the button was clicked?

All code below is placed within the content template sections for the Loop Item.

 <div class="col-sm-3">
    <button class="button" onclick="myFunction()">Add a Booking</button>
    <!--<a href="[wpv-post-url]">Go To Ticket</a>-->
  </div>
  <div class="col-sm-12" id="myDIV" style="display: none;">
    <hr>
    <h5><b>Add a Booking</b></h5>
    [cred_form form='new-booking' form_name='New Booking']
  </div>

The Javascript is as follows...

 
function myFunction() {
    var x = document.getElementById("myDIV");
    if (x.style.display === "none") {
        x.style.display = "block";
    } else {
        x.style.display = "none";
    }
}
#578599

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Jon,

Thank you for contacting Toolset support. We can’t use same ID(myDIV) multiple time on the same page, so that I’ve updated HTML and JS code.

1. Please replace the html code in the Content Template with this updated one:

<div class="frm-outer-container">
 <div class="col-sm-3">
   <button class="button btn-booking">Add a Booking</button>
   <!--<a href="[wpv-post-url]">Go To Ticket</a>-->
 </div>
 <div class="col-sm-12 frm-container" style="display: none;">
   <hr>
   <h5><b>Add a Booking</b></h5>
   [cred_form form='new-booking' form_name='New Booking']
 </div>
</div>

2. And use this updated JS code, remove the old one you have:

jQuery(document).ready(function($) {
	$('.btn-booking').on('click',function(){
		$(this).parents('.frm-outer-container').find('.frm-container').show('slow');
	});
});

I hope it will work fine then, if not please let me know and I will check further accordingly.
Thank you

#578603

This has worked, I just have one follow up issue...

Is it possible to add an option to hide the form after if is shown, or turn the 'Add a Booking' button to read 'Hide Form' and perform the hide when clicked?

#578608

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Please use this updated JS code, remove the old one you have. This will dynamically change the Label of the button and same button will work for both functions (Add & Hide):

jQuery(document).ready(function($) {
	$('.btn-booking').on('click',function(){
		$(this).parents('.frm-outer-container').find('.frm-container').slideToggle('slow');
		if( $(this).text() == 'Add a Booking' )
			$(this).text('Hide Form');
		else 
			$(this).text('Add a Booking');
	});
});

I hope this helps in resolving the issue. Have a great weekend, Thank you

#578772

Sorted, many thanks!