You are viewing limited content. For full access, please sign in.

Question

Question

How to reset value for each collection with condition

asked on November 12, 2018

Hi all!

 

I created a simple form with a simple collection (1 checkbox and 1 input).

I'm trying to change the input value if my checkbox is checked.

 

 

I'm using the javascript for this.

$(document).ready(function()
	{
    	$('.cf-collection > div').each(function ()
			{
            	$(this).find('#q3').click(function() 
					{
                      $(this).closest("div").find('#q5 input').val("00");
                    });
            });
	});

Actually, it's working for the first one but not for the second one and idk why.

 

 

What is my mistake please?

 

Thanks in advance.

Regards

0 0

Answer

SELECTED ANSWER
replied on November 12, 2018 Show version history

Was the second set of fields added by clicking "Ajouter"? If so, what's probably happening is that your code only checks for fields that existed when the document is ready, so the .each iterator fails to detect fields that are added after the page has loaded. You'll need to add a function that also checks when "Ajouter" is clicked.

1 0

Replies

replied on November 12, 2018

Glad to hear it. I was actually finishing up an alternative method to do what you wanted, based on this example.

I'll paste it here anyways in case anyone else is interested. I've assigned the custom CSS class named "Collection" to the collection, and the time field has the ID q104.

$(document).ready(function () {
  $('.Collection').on('change', '[type="checkbox"]', reqChange);
  
  function reqChange() {
    $('.Collection .rpx .radio-checkbox-fieldset').each(function () {
      $(this).find('input:checked').each(function() {
	      var reviewerField = $(this).closest('.rpx').find('[name="q104"]');
          $(reviewerField).find('input').val('00');

      })
    })
  }
})

 

2 0
replied on November 12, 2018

it seems logical ... but how can I do it?

0 0
replied on November 12, 2018

It's ok!

I found how to do.

 

Thank you very much Leif.

This is my code.

 

$(document).ready(function() 
	{
  		$('.cf-collection').change( function ()
			{
  				$('.cf-collection > div').each(function ()
					{
            			$(this).find('#q3').click(function() 
							{
                      			$(this).closest("div").find('#q5 input').val("00");
                    		});
            		});
			});
    });

 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.