2

I want to disable TableSorter plugin on some th. These th have class="disableSorter", so I do:

$('.disableSorter').each(function(index) {

    $('.tablesorter').tablesorter({ headers: { index: { sorter: false} } }); 

});

where index: is the index variable passed in the function. The thing is " index " is not replaced by the number (for example 0:, 1: 5:, etc.)

Thanks for help!

===== Edit =====

Thanks JaredPar, your solution was right about the dynamic variable. I had to edit the code because I wan't getting the index of the th element, but the index of the each loop.

Here's my final code

var inner = {};
$('.disableSorter').each(function() {
    inner[$('.tablesorter th').index(this)] = { sorter: false };
});

$('.tablesorter').tablesorter({ headers: inner  }); 
1
  • I suggest you modify the title to "Can't use dynamic variable in place of object index in JQuery Plugin call" Commented Dec 5, 2013 at 6:01

2 Answers 2

4

The problem here is you're trying to use the value in a place where javascript is not looking for values but instead for literals to use as names. You need to use the [] syntax to create a named member based on a value.

Try the following

$('.disableSorter').each(function(index) {
  var inner = {};
  inner[index] = { sorter: false };
  $('.tablesorter').tablesorter({ headers: inner); 

});
Sign up to request clarification or add additional context in comments.

Comments

1

Try this

var obj = null;
$('.disableSorter').each(function(index) {
    obj = {};
    obj.headers = {};
    obj.headers[index] = { sorter: false };
    $('.tablesorter').tablesorter(obj); 

});

Comments

Your Answer

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.