0

I'm trying to get the value $(this) with the context that the event was triggered, but I'm getting the following error:

Uncaught TypeError: Cannot read property 'toLowerCase' of undefined

cants_field_hidden = document.createElement('input')
cants_field_hidden.setAttribute 'type', 'hidden'
cants_field_hidden.setAttribute 'id', 'cant_' + if id == undefined then item_id else id
cants_field_hidden.classList.add 'cant_input'
cants_field_div.append cants_field_hidden

cants_field_visible = document.createElement('input')
cants_field_visible.setAttribute 'type', 'number'
cants_field_visible.setAttribute 'placeholder', 'Porciones'
cants_field_visible.setAttribute 'name', 'ammounts[]'
cants_field_visible.setAttribute "onkeypress", calculate_carbohydrates((if id == undefined then item_id else id), (if 
carbohydrates == undefined then item_carbohydrates else carbohydrates))
cants_field_visible.setAttribute 'required', true
cants_field_div.append cants_field_visible

function calculate_carbohydrates(id, carbohydrates){
  console.log($(this))
  console.log(id)
  console.log(carbohydrates)

  var input = document.getElementById('cant_' + id)
  var total_carbohydrates = $(this).val() * carbohydrates

  input.value = parseInt(total_carbohydrates)

  var sum = 0;
  $(".cant_input").each(function(){
    sum += Number($(this).val())
  })
  $("#total_carbohydrates").val(sum).trigger('change')
  $("#total_carbohydrates_span").text($("#total_carbohydrates").val())
}
  • Post the relevant HTML please – Ryan Wilson Jun 06 '18 at 18:12
  • 1
    Possible duplicate of [Uncaught TypeError: Cannot set property 'value' of null](https://stackoverflow.com/questions/16100543/uncaught-typeerror-cannot-set-property-value-of-null) – Jordan Running Jun 06 '18 at 18:13
  • It seems that you may have misread the error message. It does not say "Cannot set property 'val()' of null," per your title. It says "Cannot set property 'value' of null." – Jordan Running Jun 06 '18 at 18:14
  • @JordanRunning Sorry, put the wrong error, it is already edited. – Matias Carpintini Jun 06 '18 at 18:18
  • You seem to have mixed coffeescript and javascript? – Jared Smith Jun 06 '18 at 18:21
  • Where exactly in your code is this error occuring? *getting* the value wouldn't cause an error related to *setting* the value. – Kevin B Jun 06 '18 at 18:23
  • @JaredSmith No, the function has a different file than just JS – Matias Carpintini Jun 06 '18 at 18:24
  • The error is pretty self-explanatory: `Cannot set property 'value' of null`. Look at your code. Where are you setting a property named `value`? The thing you're setting the property on isn't what you think it is; it's `null`. – Jordan Running Jun 06 '18 at 18:47
  • @JordanRunning I do not understand, I try to obtain the object value of the object with which the event was triggered, in this case it is an input and I want to obtain its value, simple. – Matias Carpintini Jun 06 '18 at 18:55
  • If the error is indeed coming from the code you've posted, then that's the line it's coming from. It means that `input` is `null`, which means that `document.getElementById('cant_' + id)` returned `null`, which means no element with the given id exists. – Jordan Running Jun 06 '18 at 18:56
  • @JordanRunning Is that you do not understand me, the variable input is another element different from the one that triggers the event ... – Matias Carpintini Jun 06 '18 at 18:57
  • The variable `input` is not any element; the value of `input` is `null`. Either that or the error is coming from some other code that you haven't shown us. – Jordan Running Jun 06 '18 at 18:59

1 Answers1

0

Find a solution

cants_field_visible.addEventListener "keyup", ->
    calculate_carbohydrates.call(this, "#{if id == undefined then item_id else id}", "#{if carbohydrates == undefined then item_carbohydrates else carbohydrates}")