Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery 'attribute contains' selector with a dynamic value

Let's say that i have a variable questionId which is an integer, and i want to find tr elements that have the fragment ("question_"+questionId) in their id. How can i do this? I thought that i would be able to do it with the jquery 'attribute contains' selector.

Eg, this works, for a non-dynamic value,

$("tr[id*='quiz_question_7674']")

but, i can't work out how to plug the variable value in there. This doesn't work for example:

questionId = 7674;
$("tr[id*='quiz_question_'+questionId]")

Any ideas anyone? Is there a better way than 'attribute contains' to do it? I have the feeling i'm missing something obvious.

thanks, max

EDIT - SOLVED. doh, i am indeed missing something obvious. I keep forgetting that it's just a string, nothing more:

$("tr[id*='quiz_question_"+questionId+"']")

like image 296
Max Williams Avatar asked Mar 05 '10 14:03

Max Williams


2 Answers

You have error:

var questionId = 7674;
$("tr[id*='quiz_question_" + questionId + "']");

Notes:

  1. Please use var to declare variables.
  2. questionId is a variable. It is not part of the selector. You should concatenate questionId to the string.
like image 152
Sagi Avatar answered Sep 17 '22 23:09

Sagi


Yep, you almost had it:

var selector = "tr[id*='quiz_question_" + questionId + "']";
$(selector)
like image 31
jvenema Avatar answered Sep 20 '22 23:09

jvenema