1

Why can't I pass the num variable to this function like so:

    function getColumnData(num) {
    var colVals = $('#newtable td:nth-child(num)').map(function(){
       return $(this).text();
    }).get();
    alert(colVals);
    }

Is there an easy way around this?

Thanks.

3 Answers 3

3

You need to concatenate the value of num with the selector string:

var colVals = $('#newtable td:nth-child(' + num + ')').map(function() {
    //Do stuff
});

Currently you just have a string literal "#newtable td:nth-child(num)", instead of something that makes sense to the selector engine, such as "#newtable td:nth-child(2)".

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

Comments

3

Because your jQuery selector is a string, you need to 'build' it using concatenation, like so:

$('#newtable td:nth-child(' + num + ')')

Otherwise num is just some characters in the string literal.

Comments

1

try on line 2

var colVals = $('#newtable td:nth-child(' + num + ')').map(function(){

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.