2
\$\begingroup\$

I have two statements that look like this:

$('table tr td:nth-child(' + columnNumber + ')').toggle();
$('table tr th:nth-child(' + columnNumber + ')').toggle();

Is there a way to combine these two statements?

\$\endgroup\$
5
\$\begingroup\$

A comma.

$('table tr td:nth-child(' + rowNumber + '), table tr th:nth-child(' + rowNumber + ')').toggle();

jQuery's selectors are the same as CSS (with some stuff added here and there), so all the usual CSS selector rules apply:

$("div, h1")  // select div and h1 elements
$("div h1")   // select h1 elements that are nested somewhere within a div
$("div > h1") // select h1 elements that are direct descendants of a div

Etc.

By the way, rowNumber doesn't make sense; the code will select a given column, not a row.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.