Jquery hide everything not equal to variable

Viewed 31

I have a very simple problem, but I can't figure out how to fix it. I have a table, and when I click on the row name of the table, the row shows.

$("#tablerow" + var).show();

The variable var represents the number of the current table row. What I want, every time a new table row shows, I want to hide the row that was selected previously. I want to hide every tablerow with a var not equal to the current var. I want something like this, but this doensn't work.

$("#tablerow" + !var).hide();

Hope you guys can help. Thanks in advance.

2 Answers

You could use :not() for this:

$('table tr:not(#tablerow' + var + ')').hide();

You'll want to use an attribute starts-with selector in combination with .toggle()

$("tr[id^='tablerow']").each((_, el) => {
  $(el).toggle(el.id === `tablerow${someVar}`)
})

FYI, var is not a valid JS variable name

Related