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

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
0
kgp43
Asked:
kgp43
  • 4
1 Solution
 
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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 StanyonCommented:
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
 
kgp43Author Commented:
Ahhhh, I see - that make sense.
Works perfectly, thanks :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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