Solved

If no checkboxes on page are checked - clear value in textarea

Posted on 2011-09-06
2
151 Views
Last Modified: 2012-05-12
I need to loop through all checkboxes on page - if no are checked - then clear the value on a textfield/area, etc.? I am doing this on the check changed event for a checkbox
0
Comment
Question by:tbaseflug
2 Comments
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
what is the question? show us your current code
0
 
LVL 17

Accepted Solution

by:
sonawanekiran earned 500 total points
Comment Utility
Very simple. Check below code. When you uncheck all checkbox the value in textarea fields get cleared.
You can test below code at

http://jsfiddle.net/Wc4NY/
<!DOCTYPE html>
<html>
<head>
<title>Kiran Test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
 $(document).ready(function(){
 $('input:checkbox').click(function(){
   clear_textarea();
  });
});

function clear_textarea()
{
  var checkbox_flag = false;
  $('input:checkbox').each(function(){
    if($(this).is(':checked'))
      checkbox_flag = true;
  });
  
  if(!checkbox_flag)
    $('#mytextarea').html("");
}

</script>
</head>
<body>
  <form>
  <table>
    <tr>
      <td><input type="checkbox" checked></td>
      <td><input type="checkbox"></td>
      <td><input type="checkbox" checked></td>
      <td><input type="checkbox"></td>
      <td><input type="checkbox" checked></td>
    </tr>
    <tr>
      <td><input type="checkbox"></td>
      <td><input type="checkbox" checked></td>
      <td><input type="checkbox"></td>
      <td><input type="checkbox"></td>
      <td><input type="checkbox" checked></td>
    </tr>
    <tr>
      <td colspan="5">
        <textarea id="mytextarea" cols="30" rows="10">Sample Text Sample Text Sample Text</textarea>
      </td>
    </tr>
  </table>
</form>
</body>
</html>

Open in new window

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now