0

I used to activate a function of a selection menu with options with this :

$("[name='MyMenu']" ).selectmenu({
                select: function(){

And functions of inputs with

$("[name='MyInput']" ).keyup(function(){

And it worked fine, but now my menu is dynamically generated. So I found close questions using .on() with the closest fixed parents. It worked fine with the "click" or "keyups" for other elements that I generate dynamically. Example of the code I use for the keyup :

$("#ParentElement").on("keyup","[name='MyInput']",function(){

But didn t work with the selectmenu. I tried :

$("#ParentElement").on("selectmenu", "[name='MyMenu']",function(){

It shows no errors but the function is not executing.

$("#ParentElement").on("selectmenu", "[name='MyMenu']",select:function(){

Shows an error.

I think I have to place somewhere that 'select :' but I am no JS expert, everytime I put it somewhere it shows an error. How to make a selectmenu with a 'select : function()' work properly ?

Eli O.
  • 1,543
  • 3
  • 18
  • 27
  • Event delegation are for events, as the name implies. `selectmenu` is a method, as such you will need to invoke it manually on the dynamically added content. – Rory McCrossan Sep 25 '18 at 14:55
  • Is `"#ParentElement"` a static element? – Mark Baijens Sep 25 '18 at 15:22
  • @MarkBaijens Yes #ParentElement is static and works well for the 'keyup' and 'click' – Eli O. Sep 25 '18 at 15:24
  • Would be great if you can provide a snippet what reproduces your problem. What plugin is firing the `selectmenu` event? – Mark Baijens Sep 25 '18 at 15:25
  • Just like what @RoryMcCrossan said 30 minutes ago. Since selectmenu is a method and not an event listener, you CAN NOT use event delegation since it is not an event. You need to bind it when you add the element..... HOW is the element being added dynamically to the page? – epascarello Sep 25 '18 at 15:27
  • @RoryMcCrossan Just to be clear, because I m not sure I understand the sentences : "you will need to invoke it manually on the dynamically added content" "You need to bind it when you add the element" I add the content dynamically with .append(). So I should like, add the script into the appended part within a '' tag ? – Eli O. Sep 25 '18 at 15:33

1 Answers1

0

you may change function

$('select').on('change', function() {
  alert( this.value );
});
Jasar Orion
  • 626
  • 7
  • 26
  • So I adapted your method and tried $("#ParentElement").on("change", "[name='MyMenu']",function(){ Strangely it shows no errors but it just doesn t start the function (the alert don t start, I tried with console.log and it shows nothing too) – Eli O. Sep 25 '18 at 15:25
  • 1
    you need to get name inside the function – Jasar Orion Sep 25 '18 at 17:39
  • 1
    look here https://stackoverflow.com/questions/20701625/js-change-select-name-based-on-option-selection – Jasar Orion Sep 25 '18 at 17:39