Solved

get checked checkbox

Posted on 2010-11-30
9
497 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
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 
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 250 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 250 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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
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…

726 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