• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 158
  • Last Modified:

Limit jQuery to each table, possible?

Hi,

I use the following jQuery to change tr background color when clicking table rows (radio input).
The problem is, I have quite a few tables containing different type of data. User click a row which is then selected and marked (background change).

Is it possible to make jQuery limited by table/div name, id or class?
Background color (active) is reset when I click a row in another table.

Current:
$(document).ready(function() {
	$('input[type=radio]').change(function(){
		$('tr.active').removeClass('active');
		$(this).parents('tr').addClass('active');
	});
});

Open in new window


Maybe something like this (probably not valid):
$(document).ready(function() {
	$('#divID.input[type=radio]').change(function(){
		$('tr.active').removeClass('active');
		$(this).parents('tr').addClass('active');
	});
});

Open in new window

0
kgp43
Asked:
kgp43
1 Solution
 
Chris StanyonCommented:
Hey Kenneth,

You'll just need to grab the parent table and then filter the active rows by that. Something like this should do it:

$(document).ready(function() {
	$('input[type=radio]').change(function(){
		var activeTable = $(this).parents('table');
		$('tr.active', activeTable).removeClass('active');
		$(this).parents('tr').addClass('active');
	});
});

Open in new window

0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now