Assuming I have a wide table with many columns, and I want to add colspan=2 to:
td#2, td#10, td#15
td#3, td#11, td#16
Do I have to do it specifically:
$("table td").eq(2).attr('colspan','2')
$("table td").eq(10).attr('colspan','2')
$("table td").eq(15).attr('colspan','2')
Or should I use filter()?
Is there any shorter way?
You could do
(I’d use
.prop()instead of.attr()I think, but I need to make sure 🙂 (edit yup it’s a real property)Note that the above would work, but those jQuery extended search qualifiers like
:eq()can slow down the selection process. It might be faster to use a separate filter step after selecting just the cells.Also note that that selection (like your original code) finds the 2nd, 10th, and 15th cells in the whole table. If you wanted to set the property of the 2nd, 10th, and 15th cells on each row, you’d probably want something different.