Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string interpolation with jQuery

I'm trying to pass in the value of a variable to a jQuery selector but I can't seem to get it right. Here is what I'm working with:

jQuery

var state = $("<%= @state %>").selector;

that captures this value -> "pending"

I'm trying to pass that value into this selector:

jQuery

$(".snitches-index-header + .tags-column .#{state}_count")

As you can see I'm trying to pass the string into the jQuery selector. But this is not working as I expect. What am I doing wrong so that the selector would read:

$(".snitches-index-header + .tags-column .pending_count")

but obviously use the variable state instead of pending?

like image 345
Bitwise Avatar asked Dec 03 '22 22:12

Bitwise


2 Answers

You can concatenate the variable with the string:

$(".snitches-index-header + .tags-column ." + state + "_count")

If you are trying to use a Template literal then you need to use backticks and the dollar sign not #:

$(`.snitches-index-header + .tags-column .${state}_count`)
like image 113
MrCode Avatar answered Dec 11 '22 15:12

MrCode


its just basic js syntax issue.

  $(".snitches-index-header + .tags-column ."+state+"_count");
like image 38
tech2017 Avatar answered Dec 11 '22 15:12

tech2017