1

IDK if I'm just overlooking something but i can get this to work:

var scu = 0291285;
$('input[value=scu]').val('changed');

nothing happens, but when i try:

$('input[value=0291285]').val('changed');

the input with a value of 0291285 changes. will this method not take a var, and if so is there a work around for this?

user1114060
  • 117
  • 2
  • 7
  • 1
    possible duplicate of http://stackoverflow.com/questions/5891840/how-to-use-javascript-variables-in-jquery-selectors – Kyle Macey Jun 06 '12 at 22:33

3 Answers3

2

jQuery does not know about the scu local variable that you have declared. It thinks you want to look for an <input> whose value is the string literal 'scu'. You want this:

$('input[value=' + scu.toString() + ']').val('changed');

univerio
  • 19,548
  • 3
  • 66
  • 68
1

You need stu to be a string to keep your leading 0:

var scu = '0291285';

Then you need to use the concatenation operator (+) in your selector, to have the value that was in your variable be used:

$('input[value=' + scu + ']').val('changed');
Paul
  • 139,544
  • 27
  • 275
  • 264
0
$('input[value=' + scu + ']').val('changed');
JMC
  • 910
  • 7
  • 11