Solved

get checked checkbox

Posted on 2010-11-30
9
492 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
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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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…

828 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