0

I want to be able to use a variable inside a Jquery selector:

I have 100 values on screen, called myValue11, myValue21, myValue31 etc, and I want to be able to insert data into one of these values, based on its value

var myVariable;


//Checking if the values are empty, if so, set 'myVariable' to a numeric value
if ($('#myValue11').val() == "")
{
   myVariable = 1;
}

else if ($('#myValue21').val() == "")
{
   myVariable = 2;
}

else if ($('#myValue31').val() == "")
{
   myVariable = 3;
}

//inserting into the first empty value, by using 'myVariable'

$('#myValue+myVariable+1').val("Hello World!");  //Doesn't work

The code above doesn't work, I can't seem to find a way to insert this variable into the selector, any help would be appreciated. Thank you

Anthony
  • 53
  • 8

1 Answers1

2

Try

$('#myValue'+myVariable+'1').val("Hello World!");

You needed the extra quotes to terminate the strings.

anon
  • 816
  • 5
  • 17