-2

I have a table where each row has a checkbox inside.

<input type="checkbox" onclick="handleClickOnReduce(this)" checked="">

The handler works like this:

function handleClickOnReduce (dom) {
  console.log(dom)
  if (dom.checked === 'true') {
    console.log('checked')
  }
  if (dom.toString().includes('checked')) {
    console.log('checked')
  }
  if (stompClient) {
    var chatMessage = {
      sender: 'userRequest',
      content: [dom.parentElement.id],
      type: 'REDUCE'
    }
    stompClient.send('/app/chat.sendMessage', {}, JSON.stringify(chatMessage))
  }
}

You can see already what my question is about. I try to figure out if the checkbox of the caller of the handleClockOnReduce function is checked or unchecked.

Any idea?

Anna Klein
  • 1,906
  • 4
  • 27
  • 56

1 Answers1

1

dom.checked has the boolean value true which is different from the string "true". Either compare to true:

  if(dom.checked === true)

Or just take the boolean itslef as condition:

  if(dom.checked)

The other condition you've shown

  if (dom.toString().includes('checked'))

is very error prone as <option id="checked"> would also match.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151