0

//here is th code i am trying to but i am not nbot getting the value in input type let me know where i am doing wrong

 <script type="text/javascript">
    script for getting value from checkbox
    var array = [];
    $('input[type="checkbox"]').change(function() {
          if ($(this).prop('checked')) {
            // Add the new element if checked:
            if (array.indexOf($(this).val()) < 0) {
                array.push($(this).val());
              } 
            } else {
              // Remove the element if unchecked:
              if (array.indexOf($(this).val()) >= 0) {
                  array.splice(array.indexOf($(this).val()), 1);
                }
            }
            $('#ff_elem512').val(array.join(", "));
            console.log(array);
    });

    </script>
    </head>
    <body>

       <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="checkbox" value="1" />One
    <input type="checkbox" value="2" />Two
    <input type="checkbox" value="3" />Three
    <input type="checkbox" value="4" />Four
    <input type="checkbox" value="5" />Five
    <br />
//trying to get the value here from javascript to input id
    <input type="text" id="ff_elem512" />

</body>
6
  • your code is working OK check console for error Commented May 24, 2017 at 9:36
  • Your code is valid & working : jsfiddle.net/rb74a62a Commented May 24, 2017 at 9:36
  • works for me. console.log show the right array. Do you any errors in console? Commented May 24, 2017 at 9:37
  • yes i want error in console Commented May 24, 2017 at 9:38
  • as Milan answered. You need to include jquery before executing it, so move jquery include in header before ypur script Commented May 24, 2017 at 9:40

3 Answers 3

2

Here is the working code:

var array = [];
$('input[type="checkbox"]').change(function() {
    if ($(this).prop('checked')) {
      // Add the new element if checked:
      if (array.indexOf($(this).val()) < 0) {
          array.push($(this).val());
        } 
      } else {
        // Remove the element if unchecked:
        if (array.indexOf($(this).val()) >= 0) {
            array.splice(array.indexOf($(this).val()), 1);
          }
      }
      $('#ff_elem512').val(array.join(", "));
      console.log(array);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>

<input type="checkbox" value="1" />One
<input type="checkbox" value="2" />Two
<input type="checkbox" value="3" />Three
<input type="checkbox" value="4" />Four
<input type="checkbox" value="5" />Five
<br />

You need to add JQuery before executing your JavaScript code.

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

Comments

0

Check out this simplified code:

<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
      $('input').click(function(){
        var a = $(this).val();
        $('#ff_elem512').val(a);
      });
    });   
    </script>
  </head>
  <body>
    <input type="checkbox" value="1" />One
    <input type="checkbox" value="2" />Two
    <input type="checkbox" value="3" />Three
    <input type="checkbox" value="4" />Four
    <input type="checkbox" value="5" />Five
    <br />
    //trying to get the value here from javascript to input id
    <input type="text" id="ff_elem512" />
</body>
</html>

2 Comments

My Pleasure! - @rohitjha
how to increase the reloading time of jsp page on runtime.
0

var array = [];
$('input[type="checkbox"]').change(function() {

      if ($(this).prop('checked')) {
        // Add the new element if checked:
        if (array.indexOf($(this).val()) < 0) {
            array.push($(this).val());
          } 
        } else {
          // Remove the element if unchecked:
          if (array.indexOf($(this).val()) >= 0) {
              array.splice(array.indexOf($(this).val()), 1);
            }
        }
        $('#ff_elem512').val(array.join(", "));
});
       <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="checkbox" value="1" />One
    <input type="checkbox" value="2" />Two
    <input type="checkbox" value="3" />Three
    <input type="checkbox" value="4" />Four
    <input type="checkbox" value="5" />Five
    <br />
//trying to get the value here from javascript to input id
    <input type="text" id="ff_elem512" />

No problem doing something like this.

Comments

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.