Skip Navigation

[Resolved] JS Code not working for all loop items

This support ticket is created 3 years, 3 months 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
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: Africa/Casablanca (GMT+01:00)

This topic contains 1 reply, has 2 voices.

Last updated by Jamal 3 years, 3 months ago.

Assisted by: Jamal.

Author
Posts
#2153711

Please watch the video of the issue. and review my View Code and JS Code I am using.
hidden link

[credform]
<div class="proto">
  [cred_field field='form_messages' class='alert alert-warning']
    <label>Datum</label>
    [cred_generic_field type='textfield' field='date2' placeholder='Datum auswählen' class='datepick']
    {
    "required":1,
    "default":""
    }
    [/cred_generic_field]
    <label>Durchführende person</label>
    [cred_generic_field type='radio' field='assigned_user' class='assignedu']
    {
    "required":1,
    "default":[],
    "options":[{"value":"SH","label":"SH"},{"value":"AG","label":"AG"},{"value":"SS","label":"SS"},{"value":"Sabine Stranzl","label":"Sabine Stranzl"},{"value":"Carmen Ungar","label":"Carmen Ungar"},{"value":"Patricia Vicujnik","label":"Patricia Vicujnik"},{"value":"other","label":"Other"}],
    "value" : "SH"
    }
    [/cred_generic_field]
  <div id="others">
    <label>Durchführende person</label>
    [cred_generic_field type='textfield' field='others' placeholder='Max. 30 Zeichen']
    {
    "required":0,
    "default":""
    }
    [/cred_generic_field]

  </div>
  <div class="form-group" style="margin-top:10px;">
    [cred_field field='form_submit' output='bootstrap' value='Einreichen' class='sbtn-client']
  </div>
</div>
[/credform]
jQuery(document).ready(function($) {
    $(function () {
      $('#others').hide();

      $("input[name=assigned_user]:radio").click(function () {

          if ($('input[name=assigned_user]:checked').val() == "other") {
              $('#others').show();

          } else {
              $('#others').hide();

          }
      });
  });
});
#2153825

Hello and thank you for contacting Toolset support.

Because the form will be available for each item of the loop, you should use a class instead of an ID for the section to hide/show:

<div class="others">
    <label>Durchführende person</label>
    [cred_generic_field type='textfield' field='others' placeholder='Max. 30 Zeichen']
    {
    "required":0,
    "default":""
    }
    [/cred_generic_field]
 
  </div>

Then you will need to run the Javascript logic(hide the div, and display it based on the radio selection) run for each item of the loop. Not only for the first one. Something like this will do it:

jQuery(document).ready(function($) {
	$('.proto').each(function(){
		var _this = $(this) // get the loop item's div.proto
		var others = _this.find('.others'); // get the section to hide
		
		others.hide();
		
		_this.find("input[name=assigned_user]:radio").click(function () {
			if ( _this.find('input[name=assigned_user]:checked').val() == "other") {
				others.show();
			}else{
				others.hide();
			}
		});
	});
});

For your second question on the video(required generic field), change the default value of the field to an empty string "":

"default":""

However, for support rules, we are able to handle only one issue at a time. This helps us to bring you a better service and also helps other users to find all the information here exposed. So, if it does not help, please create a new ticket for me and ask for me on it and I'll help you with it.