0

I'm new to jquery and I have a question about how i can save the input from my checkboxes in an object when clicking on a button. For now i just want it to see it in the console. my code looks something like this:

let UserInput = [];

const addUserInput = (ev) => {
  ev.preventDefault();
  let TextInput = {
    Name: document.getElementById('exampleInputName').value, //this is working
    InfoElektronik: $('#Info-Elektronik').click(function() { //this is not?
      console.log("Checkbox1 = " + $('#Info-Elektronik').prop('checked'));
    });
  };
};
<div class="form-check">
  <input class="form-check-input" type="checkbox" name="agree" value="" id="Info-Elektronik">
  <label class="form-check-label" for="defaultCheck1">Elektronik</label>
</div>
Untherra
  • 3
  • 5
  • be clear in your question,i am not getting it – Deepak A Mar 21 '19 at 08:01
  • Welcome to StackOverflow !!!. Read this: https://stackoverflow.com/questions/426258/setting-checked-for-a-checkbox-with-jquery , and please, post your HTML code – Dani Mar 21 '19 at 08:05

1 Answers1

0

The issue is that InfoElektronik stores a function, not the actual return value. Also in your click function, you log the value to the console.

I would do it like this:

const TextInput = {
  Name: document.getElementById('exampleInputName').value
}

$('#Info-Elektronik').click(function() {
  textInput.InfoElektronik = ("Checkbox1 = " + $('#Info-Elektronik').prop('checked')).val();
})
marcobiedermann
  • 4,317
  • 3
  • 24
  • 37