2

How can I make this work? variable scope contains either Menu or Content. So i need it to find a input with the id Scope_Menu or Scope_Content depending on the variable scope.

var scope = $(this).data('scope');       
$('#frmEditPagevar').find('input[id="Scope_ + scope"]').prop("checked", true);
Mohammad
  • 21,175
  • 15
  • 55
  • 84
Mike Lammers
  • 542
  • 9
  • 27

4 Answers4

2
$('#frmEditPagevar').find('input[id="Scope_' + scope + '"]').prop("checked", true);

You were on the right track, just keep an eye on your quotes and double quotes

LordNeo
  • 1,195
  • 1
  • 8
  • 21
  • You really don't need to find :) – Suresh Atta Feb 22 '17 at 12:02
  • Thanks for the answer, it worked. I will accept you answer in 7 minutes. – Mike Lammers Feb 22 '17 at 12:03
  • @sᴜʀᴇsʜᴀᴛᴛᴀ it all depends if his IDs are really unique or not. Despite it should be unique and the other approach could work, that's not guaranteed, specially when classes and ids are generated by some kind of template engine. – LordNeo Feb 22 '17 at 12:06
1

You don't need to use .find() because id attribute is unique in document and you can use jquery $() selector to selecting it.

$('#Scope_' + scope).prop("checked", true);

However if you want to use .find() change your code to bottom code

$('#frmEditPagevar').find('input[id="Scope_'+ scope +'"]').prop("checked", true);
// or
$('#frmEditPagevar').find('#Scope_'+ scope).prop("checked", true);
Mohammad
  • 21,175
  • 15
  • 55
  • 84
1

Since you already have an id in hand, in an Ideal world, the below should work.

var scope = $(this).data('scope');       
$("#Scope_"+ scope).prop("checked", true);
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

You should your code with my below code::

var scope = $(this).data('scope');       
$('#frmEditPagevar').find('#Scope_' + scope).prop("checked", true);
Rana Ghosh
  • 4,514
  • 5
  • 23
  • 40