1

I'm using jquery(mobile) to generate a select menu which hides/shows columns of an HTML table.

Looks like this:

 container = $('<select name="toggleCols" id="toggleCols" multiple="multiple"><option value="default" data-placeholder="true">Edit</option></select>');

 $('.tableRows').each( function(i) {
     if ( $(this).is(".toggle") ) {
         var toggle = $('<option value="'+i+'">sometext'+i'+</option>');
     container.find("#toggleCols").append(toggle); 
     });

This creates the select menu, which contains one option for each table column. Selecting this option should hide/toggle the respective table column.

However, I cannot get the change event to fire:

 $("#toggleCols").change( function(){
    console.log( "change" ) 
    // do stuff
    })

If I use live('change'... and have 10 columns, I get 10 console.logs every time I select a single option. So this doesn't work either.

QUESTION
Is there a way to use change without live and get a single console.log?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
frequent
  • 27,643
  • 59
  • 181
  • 333

2 Answers2

1

Are you using the latest version of jQuery ? There was a problem with this which was fixed after jQuery 1.4.2.

See related issue here: Any alternative to jQuery change() to detect when user selects new file via dialog box in IE8?

ps. I tried to write this as a comment/question under your post but am not allowed to for some reason. Hope this helps.

Community
  • 1
  • 1
solveig
  • 81
  • 7
0

I should have removed the event binding from the loop adding the options. Then using live works fine and fires once only.

frequent
  • 27,643
  • 59
  • 181
  • 333