Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selecting element with interpolation

Is it possible to select element with interpolation? I have a var with a string

inputId = "awesomeInput"

and i would like to select input which has ID "awesomeInput". I tried to do it like i would normally do with jquery

$("#{inputId}")

console.log tells me that something was picked, but any function i try to perform on this object fails. There are no errors, but also no effects. Like this:

$("#{inputId}").show()

How can I select an element with jquery like this, and than show it?

like image 279
Leo Avatar asked Dec 12 '25 15:12

Leo


1 Answers

Given:

inputId = "awesomeInput"

Then this:

$("#{inputId}").show()

ends up as:

$("awesomeInput").show()

and that is trying to find <awesomeInput> elements, not id="awesomeInput" elements.

The # only has special meaning when it is immediately followed by { in a double quoted string so the # in "#pancakes" does not begin an interpolation nor does the first # in "##{x}".

You want this:

$("##{inputId}").show()

The first # is just a plain old hashmark, only the #{...} part will be replaced during interpolation.

like image 121
mu is too short Avatar answered Dec 15 '25 01:12

mu is too short



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!