0

My goal is to load a simple jQuery function to trim any whitespace in an admin backend filter input. The use case involves a scanner inputting codes with spaces that will turn up no results.

I have tried a couple methods involving loading the script through an XML layout update as a phtml block, or because the filter is under adminhtml/ui_component, updating the column to load in my script as a component. The methods I have attempted have failed for one reason or another, whether because the load order is failing because I've failed to properly use requirejs in this case, or because the script cannot access the element.

The script is:

define([
 'jquery'
], 
$(function () {
 $("input[name='code']").bind('input', function(){
 $(this).val(function(_, v){
 return v.replace(/\s+/g, '');
 });
 });
 })

In essence, what is the best/recommended way to load this script so that it will strip whitespace out of an adminhtml filter?

asked May 14, 2019 at 15:40

1 Answer 1

0

Can you try with this script instead ? I should work for you. I have done 2 things :

  • Added the domReady to wait for the DOM to be loaded
  • Define $ to be jQuery

This is it :

require([
 'jquery',
 'domReady!'
], function ($) {
 $("input[name='code']").bind('input', function(){
 $(this).val(function(_, v){
 return v.replace(/\s+/g, '');
 });
 });
});
answered May 14, 2019 at 15:50
2
  • Thank you! I'll mark you as the answer very shortly. Is loading the script at a phtml file between script tags and inserting it into the XML acceptable for loading this or is there a better method for this case? Commented May 14, 2019 at 16:31
  • You can add this in the phtml or you can call a js script from the layout.xml. Both ways are fine. Commented May 15, 2019 at 7:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.