Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Strange thing about variable, cat and javascript

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
Colored Smarties
Smarty n00b


Joined: 03 Jan 2014
Posts: 3

PostPosted: Fri Jan 03, 2014 9:54 pm    Post subject: Strange thing about variable, cat and javascript Reply with quote

The following code creates a list, which is done by loops, and parts of it should be manipulated by javascript. Therefore I need an individual ID with every loop, so that only some parts are manipulated. That ID comes from the variable "OptionTopID". The "OptionTopID" is concatenated with the variable "optionTopNumber", so that I get a combination of text and number. The concatenated "OptionTopID" then is transmitted to the javascript function "Write Value". But now I have a strange problem with the transmission. The alert in the "WriteValue" function only displays the none-concatenated value "optionTop" without the number. Why? If the number isn't transmitted to the javascript function, the whole thing doesn't work. Do you have any idea why ID is transmitted in the none-concatenated version?



HTML with Smarty:
Code:


{block name="frontend_index_header_javascript_jquery" append}
   <script type="text/javascript" src="{link file='frontend/_resources/javascript/project.js'}"></script>
{/block}

   {assign var='optionTopNumber' value=1}
   

   {foreach from=$sArticle.sConfigurator item=sConfigurator}
      
      {assign var='optionTopID' value='optionTop'}
      {$optionTopID|cat:$optionTopNumber}
         
         
      
         <div {if $configValue.selected}selected="selected"{/if} value="{$configValue.optionID}" class="option_config_top" id="{$optionTopID}">
                  {$configValue.optionname}{if $configValue.upprice} {if $configValue.upprice > 0}{/if}{/if}
         </div>
         
         
         
         {foreach from=$sConfigurator.values item=configValue}
               <option value="{$configValue.optionID}" class="option_config" onclick="WriteValue('{$configValue.optionname}', '{$optionTopID}')">
                  {$configValue.optionname}{if $configValue.upprice} {if $configValue.upprice > 0}{/if}{/if}
               </option>
         {/foreach}
         
         {assign var='optionTopNumber' value=$optionTopNumber+1}
         
   
   {/foreach}



javascript:
Code:

function WriteValue (obj, id) {
   
   alert(id);
   $("#" + id).text(obj);
   $("#" + id).slideUp("slow");
};
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Sat Jan 04, 2014 10:16 am    Post subject: Reply with quote

Quote:
{$optionTopID|cat:$optionTopNumber}

does not assign the appended version to optionTopID

Use
Code:
{assign var='optionTopID' value="optionTop`$optionTopNumber`"}


See http://www.smarty.net/docs/en/language.syntax.quotes.tpl
Back to top
View user's profile Send private message
Colored Smarties
Smarty n00b


Joined: 03 Jan 2014
Posts: 3

PostPosted: Sat Jan 04, 2014 11:07 am    Post subject: Reply with quote

Thanks. But when I use that solution, then the javascript function doesn't work. There's no alert anymore. I don't know why.

Update: If I change the javascript function like you can see below, than there's the alert "undefined".

Code:

function WriteValue (obj, id) {
   
   alert(id);
   $("#" + id).text(obj);
   $(".option_config").slideUp("slow");
};
Back to top
View user's profile Send private message
Colored Smarties
Smarty n00b


Joined: 03 Jan 2014
Posts: 3

PostPosted: Sat Jan 04, 2014 11:37 am    Post subject: Reply with quote

I solved the problem. I shouldn't have used "id" for a javascript-variable. If I change it to something else, it works.
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP