Skip Navigation

[Resolved] JQuery for multiple fields number

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

Problem:

Apply custom JS codes to multiple instances field in Toolset Forms.

Solution:

You can try the JS event "toolset_repetitive_field_added", for example:

https://toolset.com/forums/topic/jquery-for-multiple-fields-number/#post-1363087

Relevant Documentation:

This support ticket is created 5 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
- 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/Hong_Kong (GMT+08:00)

This topic contains 8 replies, has 2 voices.

Last updated by FelipeP5703 5 years, 1 month ago.

Assisted by: Luo Yang.

Author
Posts
#1362887

Tell us what you are trying to do?

I have a code that would style the number field into a phone number in CRED form, meaning if the user puts a number as 2199556677, it would automatically style to (21) 9955-6677, which is working fine if the field is not repeatable.

But if the field is repeatable, it only styles the first one. How do I make every additional field stylish as the first one?

fixo is my slug for the repeatable number field

Here is the code:

jQuery(function($) {
$('.fixo').on('keypress', function(e) {
  var key = e.charCode || e.keyCode || 0;
  var phone = $(this);
  if (phone.val().length === 0) {
    phone.val(phone.val() + '(');
  }
  // Auto-format- do not expose the mask as the user begins to type
  if (key !== 8 && key !== 9) {
    if (phone.val().length === 3) {
      phone.val(phone.val() + ')');
    }
    if (phone.val().length === 4) {
      phone.val(phone.val() + ' ');
    }
    if (phone.val().length === 9) {
      phone.val(phone.val() + '-');
    }
    if (phone.val().length >= 14) {
      phone.val(phone.val().slice(0, 13));
    }
  }

  // Allow numeric (and tab, backspace, delete) keys only
  return (key == 8 ||
    key == 9 ||
    key == 46 ||
    (key >= 48 && key <= 57) ||
    (key >= 96 && key <= 105));
})

.on('focus', function() {
  phone = $(this);

  if (phone.val().length === 0) {
    phone.val('(');
  } else {
    var val = phone.val();
    phone.val('').val(val); // Ensure cursor remains at the end
  }
})

.on('blur', function() {
  $phone = $(this);

  if ($phone.val() === '(') {
    $phone.val('');
  }
});
});
#1363087

Hello,

For those new items, please try the JS event "toolset_repetitive_field_added", for example, you can modify your JS codes as below and test again:

jQuery(document).on( "toolset_repetitive_field_added", function() {
    myFunction();
});
myFunction();

function myFunction() {
  jQuery(function($) {
$("input[name^='wpcf-fixo']").on('keypress', function(e) {
  var key = e.charCode || e.keyCode || 0;
  var phone = $(this);
  if (phone.val().length === 0) {
    phone.val(phone.val() + '(');
  }
  // Auto-format- do not expose the mask as the user begins to type
  if (key !== 8 && key !== 9) {
    if (phone.val().length === 3) {
      phone.val(phone.val() + ')');
    }
    if (phone.val().length === 4) {
      phone.val(phone.val() + ' ');
    }
    if (phone.val().length === 9) {
      phone.val(phone.val() + '-');
    }
    if (phone.val().length >= 14) {
      phone.val(phone.val().slice(0, 13));
    }
  }
 
  // Allow numeric (and tab, backspace, delete) keys only
  return (key == 8 ||
    key == 9 ||
    key == 46 ||
    (key >= 48 && key <= 57) ||
    (key >= 96 && key <= 105));
})
 
.on('focus', function() {
  phone = $(this);
 
  if (phone.val().length === 0) {
    phone.val('(');
  } else {
    var val = phone.val();
    phone.val('').val(val); // Ensure cursor remains at the end
  }
})
 
.on('blur', function() {
  $phone = $(this);
 
  if ($phone.val() === '(') {
    $phone.val('');
  }
});
});
}  

#1363595

Works great, thank you but a quick question: hidden link

As you can see in the video, the "Add new" button disappears if I delete the field. I'm not sure if it has to do with the limiting JS code below:

jQuery(function($) {
    $(".limit-to-2 .js-wpt-repadd").click(function(event) {
        $length = this.parentNode.childNodes.length;
        if ($length >= 2) {
            $(this).hide();
        }
    });
});
#1363953

Yes, I think so, it should due to the JS codes you mentioned above, you can simply remove those JS codes, and test again.

#1364155

But Luo, I need to limit how many phone numbers the customer can add. Toolset should have a min and max number of repeatable fields in the settings when creating a repeatable field.

Since it does not, how do I limit the number of phone numbers the customer can add without "hiding" the option?

#1365165

For the new question:
how do I limit the number of phone numbers the customer can add without "hiding" the option?

It needs custom JS codes, I can get your website credentials in your another thread:
https://toolset.com/forums/topic/array-for-repeating-field/

Where I can see the problem as your video capture:
hidden link

Please point out the problem page/post URL? I need to test it in a live website, thanks

#1365635

Lou, I have assigned a custom post named "Felipe" to your login. But the form is Post Form ID 29, when you login, go to hidden link

In the custom field named "Fixo" you will see "Add New" ... click it, it should give you and option to put the other number. Then click the trash can or the X to remove the second option and you will see that the Add New does not come back.

This is the code I have to hide it. I want to limit to 2 numbers.

//Função para limitar 2 telefones fixos
jQuery(function($) {
    $(".limit-to-2 .js-wpt-repadd").click(function(event) {
        $length = this.parentNode.childNodes.length;
        if ($length >= 2) {
            $(this).hide();
        }
    });
});
#1365969

Thanks for the details, I have done below modifications in your website:
Edit the post form "Formulário de Anúncio":
hidden link
in "JS editor", line 58~63, add below JS codes:

jQuery(document).on('click', '.limit-to-2 .js-wpt-repdelete', function (e) {
  var length = jQuery(this).parents('.js-wpt-field-items').children('.wpt-repctl').length;
  if (length <= 2) {
    jQuery(this).parents('.js-wpt-field-items').children( '.js-wpt-repadd').show();
  }
});

You can test it in front-end here:
hidden link
in field "Fixo", click link "Add new", then remove an instance, the "Add new" link will be able to display again

#1366315

Worked like a charm! My issue is resolved now. Thank you!