Saltar navegación

[Resuelto] Custom search with jQuery function

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem:

I have a custom search and I needed to change dynamically width of select box so I have found a jQuery code which does this.

Solution:

Your custom JS codes does trigger when page is loaded, for example, you can add a line, just below the first line, see details here:

https://toolset.com/forums/topic/custom-search-with-jquery-function/#post-1190278

Relevant Documentation:

https://developer.mozilla.org/en-US/docs/Web/API/Console/log

This support ticket is created hace 6 años, 1 mes. 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)

Este tema contiene 2 respuestas, tiene 2 mensajes.

Última actualización por toolset-dave hace 5 años, 11 meses.

Asistido por: Luo Yang.

Autor
Mensajes
#1189907

Hi,

I have a custom search and I needed to change dynamically width of select box so I have found a jQuery code which does this. It works after selecting some item from the select box, see enlace oculto (yellow underlined text). Can you help me to make it working immediately after the page loads?

(function($, window){

  $.fn.resizeselect = function(settings) {  
    return this.each(function() { 

      $(this).change(function(){
        var $this = $(this);

        // create test element
        var text = $this.find("option:selected").text();
        
        var $test = $("<span>").html(text).css({
        	"font-size": $this.css("font-size"), // ensures same size text
          "visibility": "hidden" 							 // prevents FOUC
        });
        

        // add to body, get width, and get out
        $test.appendTo($this.parent());
        var width = $test.width();
        $test.remove();

        // set select width
        $this.width(width);

        // run on start
      }).change();

    });
  };

  // run by default
  $("select.resizeselect").resizeselect();                       

})(jQuery, document);
#1190278

Hello,

Your custom JS codes does trigger when page is loaded, for example, you can add a line, just below the first line, like this:

(function($, window){
  console.log('This codes is triggered!'); // add this line
...

Test the page in front-end, check JS console window, you should be able to see the message: This codes is triggered

There should be some other problem in the JS codes, but according to our support policy, we don't provide custom codes support:
https://toolset.com/toolset-support-policy/

I suggest you debug the JS codes manually line by line, you can use console.log() to output the Javascript variables
enlace oculto

You can also check it with our Toolset Contractors:
https://toolset.com/contractors/

#1209669

My issue is resolved now. Thank you!