?
Solved

check box validation using jquery

Posted on 2009-04-29
1
Medium Priority
?
5,394 Views
Last Modified: 2012-05-06
Hi,

I have 2 checkbox groups.both groups have the same values.what i need is if i select a value in the first group i should not be able to select the same value in the group 2.

this is chkgroup1

<input type="checkbox" name="wow[]"> <font size="2">PHP</font><br>
<input type="checkbox" name="wow[]"> LINUX<br>
<input type="checkbox" name="wow[]"> APACHE<br>

this is group2

<input type="checkbox" name="wow1[]"> <font size="2">PHP</font><br>
<input type="checkbox" name="wow1[]"> LINUX<br>
<input type="checkbox" name="wow1[]"> APACHE<br>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
 
<body>
<table width="200" border="0">
  <tr>
    <td>
    <div style="overflow:auto;width:155px;height:300px;border:1px solid #336699;padding-left:5px">
<input type="checkbox" name="wow[]"> <font size="2">PHP</font><br>
<input type="checkbox" name="wow[]"> LINUX<br>
<input type="checkbox" name="wow[]"> APACHE<br>
<input type="checkbox" name="wow[]"> MYSQL<br>
<input type="checkbox" name="wow[]"> POSTGRESQL<br>
<input type="checkbox" name="wow[]">SQLITE<br> </div> 
    
    </td>
    <td> <div style="overflow:auto;width:155px;height:300px;border:1px solid #336699;padding-left:5px">
<input type="checkbox" name="wow1[]"> <font size="2">PHP</font><br>
<input type="checkbox" name="wow1[]"> LINUX<br>
<input type="checkbox" name="wow1[]"> APACHE<br>
<input type="checkbox" name="wow1[]"> MYSQL<br>
<input type="checkbox" name="wow1[]"> POSTGRESQL<br>
<input type="checkbox" name="wow1[]">SQLITE<br> </div>
</td>
</tr>
</table>
   </body>
</html>

Open in new window

0
Comment
Question by:whspider
1 Comment
 
LVL 1

Accepted Solution

by:
TECHHEAD_biz earned 2000 total points
ID: 24288924
You needed to add the 'value' attribute to your input type="checkbox" elements. I have reposted your HTML with the appropriate jQuery code added.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
 
// returns true if there is a checked checkbox
// of the given name with the given value
function isChecked(name, value) {
  var elms = $("input[name='" + name + "']:checked");
  for (var i=0; i<elms.length; i++) {
    if (elms[i].value == value) return true;
  }
  return false;
}
 
$(document).ready(function() {
 
  // will only allow a checkbox in group 'wow1'
  // to be checked if it is not checked in 'wow2'
  $("input[name='wow1']").click(function() {
    return !isChecked('wow2', this.value);
  });
 
  // will only allow a checkbox in group 'wow2'
  // to be checked if it is not checked in 'wow1'
  $("input[name='wow2']").click(function() {
    return !isChecked('wow1', this.value);
  });
});
 
</script>
</head>
 
<body>
<table width="200" border="0">
  <tr>
    <td>
    <div style="overflow:auto;width:155px;height:300px;border:1px solid #336699;padding-left:5px">
<input type="checkbox" name="wow1" value="PHP"> <font size="2">PHP</font><br>
<input type="checkbox" name="wow1" value="LINUX"> LINUX<br>
<input type="checkbox" name="wow1" value="APACHE"> APACHE<br>
<input type="checkbox" name="wow1" value="MYSQL"> MYSQL<br>
<input type="checkbox" name="wow1" value="POSTGRESQL"> POSTGRESQL<br>
<input type="checkbox" name="wow1" value="SQLITE">SQLITE<br> </div> 
    
    </td>
    <td> <div style="overflow:auto;width:155px;height:300px;border:1px solid #336699;padding-left:5px">
<input type="checkbox" name="wow2" value="PHP"> <font size="2">PHP</font><br>
<input type="checkbox" name="wow2" value="LINUX"> LINUX<br>
<input type="checkbox" name="wow2" value="APACHE"> APACHE<br>
<input type="checkbox" name="wow2" value="MYSQL"> MYSQL<br>
<input type="checkbox" name="wow2" value="POSTGRESQL"> POSTGRESQL<br>
<input type="checkbox" name="wow2" value="SQLITE">SQLITE<br> </div> 
</td>
</tr>
</table>
   </body>
</html>

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
Suggested Courses
Course of the Month17 days, 1 hour left to enroll

864 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