rohit jha
rohit jha

Reputation: 3

not getting value from javascript in input type via checkbox

//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>

Upvotes: 0

Views: 84

Answers (3)

foobar
foobar

Reputation: 619

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>

Upvotes: 0

Kerim Ayturk
Kerim Ayturk

Reputation: 106

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.

Upvotes: 0

Milan Chheda
Milan Chheda

Reputation: 8249

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.

Upvotes: 2

Related Questions