1

I have a html form that has check box and text area. When I check the check box, I want it to be dynamically update the text area with its value. what am I doing wrong? here is the code:

<html>
<head>
<meta charset="ISO-8859-1">
<title>Sample Web page</title>
<script type="javascript">
   function changeTA() {
   var inputElements = document.getElementByName("favorite_pet");
       for(var i=0; inputElements[i]; ++i){
           if(inputElements[i].checked){
               $('#ta').val($('#ta').val()+inputElements[i].value); 
     }
   }
</script>
</head>
<body>
<form>  
   <legend>What is Your Favorite Pet?</legend>  
   <input type="checkbox" name="favorite_pet" id="check_cat" value="Cats" onchange="changeTA()">Cats<br>  
   <input type="checkbox" name="favorite_pet" id="check_dog" value="Dogs" onchange="changeTA()">Dogs<br>  
   <input type="checkbox" name="favorite_pet" id="check_bird" value="Birds" onchange="changeTA()">Birds<br>  
       <br>  

   <textarea id="ta" rows="4" cols="50">
   </textarea>
</form>

</body>
</html>

1 Answer 1

2

You have 3 problems in your code:

  • You have a typo in getElementByName, use getElementsByName
  • You're missing a closing bracket in your function
  • Change <script type="javascript"> to <script type="text/javascript">

Your code should work with these 3 problems fixed:

<html>
<head>
<meta charset="ISO-8859-1">
<title>Sample Web page</title>
<script type="text/javascript">
   function changeTA() {
        var inputElements = document.getElementsByName("favorite_pet");
        for(var i=0; inputElements[i]; ++i){
           if(inputElements[i].checked){
               $('#ta').val($('#ta').val()+inputElements[i].value); 
              }
       }
   }
</script>
</head>
<body>
<form>  
   <legend>What is Your Favorite Pet?</legend>  
   <input type="checkbox" name="favorite_pet" id="check_cat" value="Cats" onchange="changeTA()">Cats<br>  
   <input type="checkbox" name="favorite_pet" id="check_dog" value="Dogs" onchange="changeTA()">Dogs<br>  
   <input type="checkbox" name="favorite_pet" id="check_bird" value="Birds" onchange="changeTA()">Birds<br>  
   <br>  

   <textarea id="ta" rows="4" cols="50">
   </textarea>
</form>

</body>
</html>

Here's a working fiddle: https://jsfiddle.net/mrz1L4h9/

Sign up to request clarification or add additional context in comments.

10 Comments

still didn't get the result I expected. the textarea box is blank
you're right, you're also missing a closing bracket in your script, I updated my answer :)
thank you. it s working in fiddle but not in chrome / IE. any idea? why?
no..what I meant is a webpage is supposed to be run on a browser directly without any tools like fiddle. it works in fiddle but not directly in the browser. any reason that you are aware of?
JSFiddle is not a tool to run your javascript code, it's just an "online IDE" that is great for sharing code snippets, all the code you write there runs on the browser.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.