jQuery onClick error

Hi,

I have a problem with label tags removing/adding wrong classes.
It's seems random and I have no idea what is causing it, jQuery is still new to me.

jQuery should add/remove a class to <label> under correct parent div id (sketch_yellow / sketch_green)

See screenshot below.

jQuery:
<script>

	$(document).ready(function() {
		
		$('input[type=radio]').change(function(){
			var activeLabel = $(this).parents('.sketch_yellow');
			$('label.yellow_active', activeLabel).removeClass('yellow_active');
			$(this).parents('label').addClass('yellow_active');
		});
		$('input[type=radio]').change(function(){
			var activeLabel = $(this).parents('.sketch_green');
			$('label.green_active', activeLabel).removeClass('green_active');
			$(this).parents('label').addClass('green_active');
		});		
	});
</script>

Open in new window


HTML:
<div class="sketch_yellow">
	<label for="y1" class="radio_btn yellow_btn radio1 <?php if($conf['connection_port_bot_angle'] == 0) echo 'yellow_active'; ?>">
		<input id="y1" name="connection_port_bot_angle" value="0" type="radio" class="r-input" <?php cps_checked(0, $conf['connection_port_bot_angle']); ?>>
	</label>
	<label for="y2" class="radio_btn yellow_btn radio2 <?php if($conf['connection_port_bot_angle'] == 45) echo 'yellow_active'; ?>">

		<input id="y2" name="connection_port_bot_angle" value="45" type="radio" class="r-input" <?php cps_checked(45, $conf['connection_port_bot_angle']); ?>>
	</label>
	<label for="y3" class="radio_btn yellow_btn radio3 <?php if($conf['connection_port_bot_angle'] == 90) echo 'yellow_active'; ?>">
		<input id="y3" name="connection_port_bot_angle" value="90" type="radio" class="r-input" <?php cps_checked(90, $conf['connection_port_bot_angle']); ?>>
	</label>
</div>

Open in new window


bghashdas.png
kgp43Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HugoHiaslCommented:
You added 2 different onchange handler to the same element.  Is this by intention? The second one overwrites the first one. Why not doing both checks in the same handler?
0
kgp43Author Commented:
The onchange need to remove a class if it already exist and add a new one.

Example:

Page is loaded and the current selected one (value from database) is selected.
User click a new radio and the previous one is "unchecked" (class removed) and new one is selected (class added).
0
kgp43Author Commented:
Also, the "radio box" to the left works fine, as stand alone.
When I add jQuery for the "radio box" to the right, then it all start to mess up (both left and right)
0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

kgp43Author Commented:
Any suggestions? This is the HTML for the "radio-box" on the right side of the page:

<div class="sketch_green">
	<label for="g1" class="radio_btn green_btn radio1 <?php if($conf['connection_port_top_angle'] == 0) echo 'green_active'; ?>">
		<input id="g1" name="connection_port_top_angle" value="0" type="radio" class="r-input" <?php cps_checked(0, $conf['connection_port_top_angle']); ?>>
	</label>
	<label for="g2" class="radio_btn green_btn radio2 <?php if($conf['connection_port_top_angle'] == 45) echo 'green_active'; ?>">
		<input id="g2" name="connection_port_top_angle" value="45" type="radio" class="r-input" <?php cps_checked(45, $conf['connection_port_top_angle']); ?>>
	</label>
	<label for="g3" class="radio_btn green_btn radio3 <?php if($conf['connection_port_top_angle'] == 90) echo 'green_active'; ?>">
		<input id="g3" name="connection_port_top_angle" value="90" type="radio" class="r-input" <?php cps_checked(90, $conf['connection_port_top_angle']); ?>>
	</label>
	...................
</div>

Open in new window

0
Chris StanyonWebDevCommented:
Currently both change() events are being bound to EVERY radio button, so even when you click on one on the yellow side, it will fire the event for the green side. You need to filter the radio buttons for each event:

$('input[type=radio]', '.sketch_yellow').change(function(){
...

and

$('input[type=radio]', '.sketch_green').change(function(){
...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kgp43Author Commented:
Ahhhh, I see - that make sense.
Works perfectly, thanks :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.