1

how to get the first element of an array which passed to smarty template page, and treat it some way in smarty page.

i use this way but it's not working.

$("item_".{$categories.category_id[1]}).addClass("active");
Safe To be
  • 11
  • 4
  • try `[0]`? no idea if you can actually access elements by index like that in smarty though. – user428517 Jul 30 '13 at 13:57
  • possible duplicate of [Smarty how to get a first index from foreach?](http://stackoverflow.com/questions/14056545/smarty-how-to-get-a-first-index-from-foreach) – We0 Jul 30 '13 at 14:02
  • 1
    What you're trying is correct, but try this syntax as well: `$categories.category_id.1` (if 1 is the actual index of your first element, and not 0) – periklis Jul 30 '13 at 14:04
  • 1
    The smarty tag renders server side so the jquery selector would look like so `$("item_".5)`, which is not valid. You could do it like this instead `$("item_{$categories.category_id[1]}")`. The first element can be accessed like this `{$categories.category_id|reset}` (tested with smarty 3.1.14) – sofl Jul 30 '13 at 15:42

2 Answers2

0

try with concatenating strings with the + operator

For example:

var myId = "domElement";
$("#"+myId).addClass("active");
saeta
  • 4,048
  • 2
  • 31
  • 48
0

This should work:

  1. replace [1] by .1
  2. Write the Smarty variable within the "-quotes.

Result:

$("item_{$categories.category_id.1}").addClass("active");
JochenJung
  • 7,183
  • 12
  • 64
  • 113