Skip Navigation

[Resolved] JavaScript working only on first post in view loop

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

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

This topic contains 4 replies, has 2 voices.

Last updated by Waqar 1 year, 2 months ago.

Assisted by: Waqar.

Author
Posts
#2668979
Screenshot_4.jpg
Screenshot_3.jpg
Screenshot_2.jpg

I have single field that has dynamic value, that value represents the amount of stars user gave to the review, in javascript i retrieve that number using getElementById and store it in variable 'dinamicka', then i retrieve element and store it in variable y , after that i set attribute of element to be the value of dynamic number, example(single field has value 4 so user sent 4 stars, i retrieve 4 and store it in variable dinamcika and after that I set attribute to my element to have attribute data-star="4" which will create desired 4 colored stars out of 5 using css), this code works but only for first post even tho single field has dynamic value.

#2669113

Hi,

Thank you for contacting us and I'd be happy to assist.

A page can have only one unique element with an ID, so this won't work for the repeating items coming from a view.

You can replace the use of 'id' with 'class' instead since a page can have multiple elements with the same class names.

In your custom script too, you'll then replace 'getElementById' with 'getElementsByClassName'.

regards,
Waqar

#2669215
Screenshot_7.jpg
Screenshot_6.jpg
Screenshot_5.jpg

I have tried updating code like you said, but now it doesn't retrieve value from my <i class="zvezde"> element by using getElementsByClassName for some reason.

#2669547

To troubleshoot this, I'll need to see how the markup and the custom script are set up in the admin area.

Can you please share temporary admin login details, along with the link to the page where these ratings can be seen?
( please revert back to the HTML and code that was previously working, even only for the first instance )

Note: Your next reply will be private and making a complete backup copy is recommended before sharing the access details.

#2669933

Thank you for sharing these details.

Looking into the HTML structure, you can update your custom script to:


jQuery( document ).on( 'ready js_event_wpv_pagination_completed js_event_wpv_parametric_search_results_updated', function( event, data ) {
	jQuery('div[class*="js-wpv-view-layout-2275-"] .js-wpv-loop-wrapper .wpv-block-loop-item').each(function () {
		var $this = jQuery(this);
		var y = $this.find('i.zvezde');
		var x = $this.find('div.rejting');
		var dinamicka = x.text();
		jQuery(y).attr("data-star",dinamicka);
	});
});

Note: Don't forget to change ids with classes, in the HTML.

#2669947

This works perfectly now, you are brilliant Waqar, much appreciated your help!