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

Javascript function availability

 
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
klintistwood
Smarty n00b


Joined: 21 Jun 2014
Posts: 2

PostPosted: Tue Jul 29, 2014 6:50 pm    Post subject: Javascript function availability Reply with quote

Hello,

While playing with Smarty and JS, I bumped into a problem. A JS function declared in my main page is not available in my tpl files when I load them.

So more details...

Main file
<script>
$(document).ready(
function ButtonManager()
{
$( "button" ).click(function()
{ alert ('clicked'); }
});
</script>
<button>TEST</button>

in this case, when I click on the button, I get the alert as expected.

If I now add a div:
<div id="output"></div>
Then I use Ajax to get content from the server, I send back a tpl file with the same button in it. Everything is then stored in the previously created div. When I click on this button, nothing happens as if the function was not known by the tpl file. I can imagine that the tpl file alone doesn't know the function but how could I use a function declared in the main document ?

Thanks
Laurent
Back to top
View user's profile Send private message
epnb
Smarty Rookie


Joined: 24 Sep 2014
Posts: 8

PostPosted: Wed Sep 24, 2014 10:18 am    Post subject: Reply with quote

Hello Klintistwood,

This behaviour is perfectly expected. The document ready method register the "click" method only at first load of the page before any ajax call.

You need to invoke the "live" method. Take a look at :
http://stackoverflow.com/questions/10394354/applying-jquery-click-to-ajax-generated-html
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