Skip Navigation

[Résolu] Custom search with jQuery function

Ce fil est résolu. Voici une description du problème et la solution proposée.

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 Il y a 5 années et 10 mois. 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

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)

Ce sujet contient 2 réponses, a 2 voix.

Dernière mise à jour par toolset-dave Il y a 5 années et 9 mois.

Assisté par: Luo Yang.

Auteur
Publications
#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 lien caché (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
lien caché

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

#1209669

My issue is resolved now. Thank you!