0

I want to check a checkbox whose name and value I know, but I cannot use it this way. How can I select with 2 values?

$('[name="' + key + '"  value="' + value[i] + '"]').attr('checked', true);
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
yukselm
  • 13
  • 2
  • 3
    `$("[attr=val][attr2=val2]")` no space and each attribute in its own `[]` – freedomn-m Aug 18 '21 at 08:44
  • oops.. I thought I tried that, it did. – yukselm Aug 18 '21 at 08:47
  • Didn't notice my own comment linking to my own answer with more explanation :) [select an element with two or more conditions](https://stackoverflow.com/questions/37136123/jquery-select-an-element-when-two-or-more-conditions-are-true) – freedomn-m Aug 18 '21 at 08:55

1 Answers1

2

Join your attribute selectors [][]

$(`[name="${key}"][value="${value[i]}"]`).prop({checked: true});

The above uses the more readable Template Literals instead of string concatenation using +

Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313