2

I'm a complete jQuery noob. I'm grabbing button_types and and div variables but this is giving me the error:

<script>
    $(function(){
      $('div [button_type="'type'"]').addClass('active');
      $('div [image-var="'image'"]').addClass('active');
    });
    </script>

I've tried putting the ) in a multitude of locations but I'm guessing there's a bigger issue with my syntax that's causing this.

Probably an easy solution for someone that knows jQuery.

Jake
  • 1,328
  • 1
  • 21
  • 51
  • So yeah. All comments have noted that I didn't throw in concatenation. Thanks everyone. – Jake Dec 06 '17 at 20:27
  • Select one as accepted. I'm inclined to suggest T. Piscaglia to help get up that sweet site rep. :) – disinfor Dec 06 '17 at 20:28
  • https://jsfiddle.net/qyfo59kr/ I'm not sure if this is limited to ES6 or what, but you can also use, I think it's called, string interpolation. Edit: template literals, and yah, it's ES6, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals – Taplar Dec 06 '17 at 20:29
  • @Taplar yeah, that's ES6 https://stackoverflow.com/questions/41829275/what-is-variable-in-javascript - pretty cool though! – disinfor Dec 06 '17 at 20:31

3 Answers3

4

You're missing the + signs with your variables:

$('div [button_type="' + type + '"]').addClass('active');
$('div [image-var="' + image + '"]').addClass('active');
disinfor
  • 10,865
  • 2
  • 33
  • 44
3
$('div [button_type="'type'"]')

As soon as the parser hits the ' before type, it terminates the string. But then you have it followed by more text which is invalid syntax. It's not entirely clear if you meant type to be a variable or not, but if so, it would look like

$('div [button_type="'+ type +'"]')
Taplar
  • 24,788
  • 4
  • 22
  • 35
3

You're missing + when concatenating. Should be

$(function(){
      $('div [button_type="' + type + '"]').addClass('active');
      $('div [image-var="' + image +'"]').addClass('active');
    });
T. Piscaglia
  • 50
  • 1
  • 5