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,980
  • 2
  • 22
  • 34
Gowtham Koushik
  • 274
  • 1
  • 6
  • 14

2 Answers2

1

Try this

var name="Jack";
$("input[type=radio][name="+name+"]").attr('disabled', true);
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
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
  • 5,586
  • 1
  • 21
  • 32