0

So I'm trying to use this plain simple code:

$(`#${id}`).css({
    `position`: `fixed`
}).width(width).height(height);

However, the middle line is throwing me an error on execution: Uncaught SyntaxError: Unexpected template string. I'm not sure what else I can add onto this question of why it's happening, so I'll let you guys with it.

  • Can you show the stack trace? (the line numbers on which the error happened). Also, try replacing `\`position\`` with `"position"` and `\`fixed\`` with `"fixed"` – vityavv Mar 08 '18 at 14:06
  • Possible duplicate of [Template String As Object Property Name](https://stackoverflow.com/questions/33194138/template-string-as-object-property-name) – Titus Mar 08 '18 at 14:07
  • Can not replicate the error [jsfiddle](https://jsfiddle.net/6nu4evx8/8/), could you provide a value for `id`. – finw3 Mar 08 '18 at 14:36
  • @finw3 That is because you're passing two string parameters to `css(...)` instead of an object. https://jsfiddle.net/6nu4evx8/9/ – Titus Mar 08 '18 at 14:38

1 Answers1

-1

You have used wrong jquery syntax try below.

Note: I hope you have properly used width and height parameters.

$("#id").css({
    "position": "fixed"
}).width(width).height(height)
  • 1
    Could you explain why this is the wrong syntax though? – Luicy Willowfield Mar 08 '18 at 14:10
  • Are you trying to change this id dynamically? I've given the solution for a fix id. – Pranjal Chaudhari Mar 08 '18 at 14:12
  • 1
    Sure you gave me a 'solution', however based of your answer, you only said that I'm using the wrong JQuery syntax, and I'd like to know why this syntax is wrong. Because it does work with strings given with parameters, not to mention you removed my variable from the JQuery object element selector. – Luicy Willowfield Mar 08 '18 at 14:14
  • 2
    @LuicyWillowfield Is not wrong jQuery syntax is wrong JavaScript syntax, you cannot use template literals as property names in object literals. – Titus Mar 08 '18 at 14:17