?
Solved

get checked checkbox

Posted on 2010-11-30
9
Medium Priority
?
501 Views
Last Modified: 2013-11-05
I have 5 checkboxes:

<input type="checkbox" name="places_of_interest_ids[]" id="places_of_interest_ids" value="1"> None
<input type="checkbox" name="places_of_interest_ids[]" id="places_of_interest_ids" value="2"> Option 1
<input type="checkbox" name="places_of_interest_ids[]" id="places_of_interest_ids" value="3"> Option 2
<input type="checkbox" name="places_of_interest_ids[]" id="places_of_interest_ids" value="4"> Option 3
<input type="checkbox" name="places_of_interest_ids[]" id="places_of_interest_ids" value="5"> Option 4

If the user checked None, then uncheck all other options, leaving None checked.  

If the user checks a box other than None, check the selected checkbox and uncheck None.


However I cant get it working.


$("input:checkbox[name=places_of_interest_ids]").bind("click",function()
{
	if($("input:checkbox[name=places_of_interest_ids]").is(":checked") && $("input:checkbox[name=places_of_interest_ids]").val() == 1) 
	{
		$("input:checkbox[name=places_of_interest_ids]").each(function() 
		{ 
			$("input:checkbox[name=places_of_interest_ids]").attr("checked",false); 
		}); 
					
		$("input:checkbox[name=places_of_interest_ids]").attr("checked",true);
	}
	else if($("input:checkbox[name=places_of_interest_ids]").is(":checked") && $("input:checkbox[name=places_of_interest_ids]").val() > 1)
	{
		$$("input:checkbox[name=places_of_interest_ids]:first").attr("checked",false); 
	}
},function(){});

Open in new window

0
Comment
Question by:ellandrd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 9

Expert Comment

by:Shahzad Fateh Ali
ID: 34240968
$(function(){
 $('.places_of_interest_ids').click(function(){
    $('.places_of_interest_ids').removeAttr('checked');
    $(this).attr('checked','checked');
 });
});

Open in new window


Change id attr to class
<input type="checkbox" name="places_of_interest_ids[]" class="places_of_interest_ids" value="1"> None
<input type="checkbox" name="places_of_interest_ids[]" class="places_of_interest_ids" value="2"> Option 1
<input type="checkbox" name="places_of_interest_ids[]" class="places_of_interest_ids" value="3"> Option 2
<input type="checkbox" name="places_of_interest_ids[]" class="places_of_interest_ids" value="4"> Option 3
<input type="checkbox" name="places_of_interest_ids[]" class="places_of_interest_ids" value="5"> Option 4

Open in new window

0
 
LVL 6

Expert Comment

by:ventaur
ID: 34240972
First, I suggest you have unique ids for your elements; it will make things easier. However, here is some code that should do the trick regardless of your ids.

$('input:checkbox[name^=places_of_interest_ids]').click(function () {
	var $this = $(this);
	if ($this.is(':checked')) {
		if ($this.val == '1') {
			$('input:checkbox[name^=places_of_interest_ids][val!=1]').attr('checked', false);
		} else {
			$('input:checkbox[name^=places_of_interest_ids][val=1]').attr('checked', false);
		}
	}
});

Open in new window

0
 
LVL 16

Author Comment

by:ellandrd
ID: 34241152
Hi

Thanks for the comments.

Have these solutions been tested? The reason I ask is I'm not in the office at the moment and won't be for another few hours. If they have been tested and still work I can accept solution...
0
Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

 
LVL 6

Expert Comment

by:ventaur
ID: 34241463
With a couple minor typo adjustments, mine works as below (tested).

$('input:checkbox[name^=places_of_interest_ids]').click(function (e) {
	var $this = $(this);
	if ($this.is(':checked')) {
		if ($this.val() == '1') {
			$('input:checkbox[name^=places_of_interest_ids][value!=1]').attr('checked', false);
		} else {
			$('input:checkbox[name^=places_of_interest_ids][value=1]').attr('checked', false);
		}
	}
});

Open in new window

0
 
LVL 16

Author Comment

by:ellandrd
ID: 34241559
this line:

$this.val()

is returning:

object: [Object]

0
 
LVL 6

Expert Comment

by:ventaur
ID: 34241971
With the check boxes you listed (I copied and pasted them) on an otherwise blank HTML page and my script block, all works without issue. Note, I am using jQuery 1.4.2.

Perhaps there is something else on the page with a name that begins with 'places_of_interest_ids'?
0
 
LVL 16

Author Comment

by:ellandrd
ID: 34242000
Can you post all your code here please?
0
 
LVL 19

Accepted Solution

by:
Albert Van Halen earned 1000 total points
ID: 34242121
I'd suggest having the markup like this
<input type="checkbox" name="places_of_interest_ids[]" id="none" value="1"> None
<input type="checkbox" name="places_of_interest_ids[]" class="other" value="2"> Option 1
<input type="checkbox" name="places_of_interest_ids[]" class="other" value="3"> Option 2
<input type="checkbox" name="places_of_interest_ids[]" class="other" value="4"> Option 3
<input type="checkbox" name="places_of_interest_ids[]" class="other" value="5"> Option 4

Open in new window

then use the following code...
$(function() {
    $("#none").click(function() {
        if(this.checked)
            $(".other").attr("checked", false);
    });
    $(".other").click(function() {
        if(this.checked)
            $("#none").attr("checked", false);
    });
});

Open in new window

Check a working example here : http://jsfiddle.net/SDZeE/
0
 
LVL 6

Assisted Solution

by:ventaur
ventaur earned 1000 total points
ID: 34242842
Here is my test page that checks out okay.

<html>
<head>
	<title>Test</title>
	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
</head>
<body>
	<input type="checkbox" name="places_of_interest_ids[]" id="places_of_interest_ids" value="1"> None
	<input type="checkbox" name="places_of_interest_ids[]" id="places_of_interest_ids" value="2"> Option 1
	<input type="checkbox" name="places_of_interest_ids[]" id="places_of_interest_ids" value="3"> Option 2
	<input type="checkbox" name="places_of_interest_ids[]" id="places_of_interest_ids" value="4"> Option 3
	<input type="checkbox" name="places_of_interest_ids[]" id="places_of_interest_ids" value="5"> Option 4
	
	<script type="text/javascript">
		$('input:checkbox[name^=places_of_interest_ids]').click(function (e) {
			var $this = $(this);
			if ($this.is(':checked')) {
				if ($this.val() == '1') {
					$('input:checkbox[name^=places_of_interest_ids][value!=1]').attr('checked', false);
				} else {
					$('input:checkbox[name^=places_of_interest_ids][value=1]').attr('checked', false);
				}
			}
		});
	</script>
</body>
</html>

Open in new window

0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question