3

How to add the Jquery or Javascript variable into the input attribute. Whether it is possible or not?

var name="Jack";
$("input[type=radio][name=name]").attr('disabled', true);
Kunj
  • 1,890
  • 2
  • 24
  • 32
Gowtham Koushik
  • 274
  • 5
  • 14

2 Answers2

1

Try this

var name="Jack";
$("input[type=radio][name="+name+"]").attr('disabled', true);
Kamil Kiełczewski
  • 71,169
  • 26
  • 324
  • 295
0

You have to use quotes around your attribute selector value. Further, name=name does not "insert" the variable's value as you intend to. You can use backticks (template literals) to do so. The following example only selects the input with name="foo" (jQuery $ does the same):

const name = "foo"
console.log(document.querySelector(`input[type="radio"][name="${name}"]`))
<input type="radio" name="foo" />

<input type="radio" name="bar" />
lipp
  • 4,928
  • 1
  • 19
  • 31