1

I know its a silly question to ask, but may be I am missing something. So, I have

data = "ajey"

and then,

I want to select an li item from ul whose id is "ajey"

I am trying with

$('#users li#data').css('background-color', 'red');

but this returns an empty array. I know the mistake is in li#data

#users is the id of the ul element

Any help is greatly appreciated.

4 Answers 4

2

since ID's are unique, you can just to:

$('#' + data).css('background-color', 'red');
Sign up to request clarification or add additional context in comments.

Comments

2

You can just concatenate the strings with + as you would normally do:

$('#users li#' + data).css('background-color', 'red');

If you are doing this, make sure that the value of data is trusted.

Comments

2

Use

$('#users li#' + data).css('background-color', 'red');

Comments

2
$('#users li#'+data).css('background-color', 'red');

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.