1

I am trying to use a function .isChecked() from the OnsenUI framework on a name.

My HTML is dynamic, so the number always changes. It's like:

<input type="checkbox" class="switch__input switch--list-item__input 
ng-dirty ng-valid-parse ng-not-empty" ng-model="model" name="menu_switch_35399">

If I run it like:

console.log(menu_switch_35399.isChecked());

it WORKS FINE and returns true or false correctly. But when I try to access it dynamically, it doesn't. What am I doing wrong?

I tried:

var test = $('[name=menu_switch_' + id + ']');
// console.log returns menu_switch_35399

And then running

test.isChecked()

but that doesn't work. I also tried:

$('[name=menu_switch_' + id + ']').isChecked()

And that also doesn't work.

The error is always

Uncaught TypeError: test.isChecked is not a function

How can I access this dynamic variable?

moo moo
  • 476
  • 5
  • 20
  • You're missing some quotes: `$('[name="menu_switch_' + id + '"]')` https://api.jquery.com/attribute-equals-selector/ – VVV Aug 04 '20 at 03:20
  • Did you mean `.checked` or `.prop('checked')` by chance? Assuming your ID variable is valid, it should work. – ChewySalmon Aug 04 '20 at 03:20
  • @VVV did not work unfortunately. `console.log($('[name="menu_switch_' + id + '"]').isChecked());` doesn't work. – moo moo Aug 04 '20 at 03:22
  • 1
    `$('[name=menu_switch_' + id + ']').is(":checked")` OR `$('[name=menu_switch_' + id + ']')[0].checked` – Mohamed-Yousef Aug 04 '20 at 03:23
  • @Mohamed-Yousef That worked! All the others didn't. If you create an answer I will accept it. Thank you also to ChewySalmon .checked was close but didn't work. – moo moo Aug 04 '20 at 03:25
  • Glad it helps @moomoo .. you're totally welcome -- Have a great day :-) – Mohamed-Yousef Aug 04 '20 at 03:28

0 Answers0