0

I want to use a variable in select of Jquery like following:

 var elemName="student_"+student_id;
 $("select[name=elemName]")[0].value=1
 $("select[name=elemName]")[1].value=2

But this above code is not working. I want to use the variable inside above select statement. So if anyone help me to do this , I will be really grateful. Thank you.

3 Answers 3

4

Try this

$("select[name='"+elemName+"']")[0].value=1
Sign up to request clarification or add additional context in comments.

1 Comment

This is perfect. You are adding quotes also for string. Which is missing in others.
1

Use it as

$("select[name="+elemName+"]")[0].value=1

Comments

0

you need to use string concatenation:

$("select[name=" + elemName + "]")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.