9three Posted October 5, 2009 Share Posted October 5, 2009 Hey, I have a link that contains a javascript function: <a href="#" onclick="refresh('<?php echo $_GET['place']?>');return false;" id="refresh"> What I need to do is change that function refresh() completely out with another function that I have. I tried doing the following, but I failed: $('#refresh').click(function () { $('#refresh').bind('onclick', getSorted()); //also tried attr }); I may have the syntax wrong but when I did try to implement it, I clicked on the link and it lagged my screen for like 3 seconds, and it didn't change out my function. Does anyone know how to replace a onclick function with a completely different one? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/176607-changing-a-function-dynamically/ Share on other sites More sharing options...
RichardRotterdam Posted October 5, 2009 Share Posted October 5, 2009 I'm just going to assume you're using jQuery since it does look that way. // remove the event from the link $('#refresh').unbind('click', refresh); // add new event $('#refresh').bind('click', getSorted()); http://docs.jquery.com/Events/unbind#type Quote Link to comment https://forums.phpfreaks.com/topic/176607-changing-a-function-dynamically/#findComment-931026 Share on other sites More sharing options...
9three Posted October 5, 2009 Author Share Posted October 5, 2009 Yes, I am using jQuery. Are you saying I need to unbind then bind the new function? Quote Link to comment https://forums.phpfreaks.com/topic/176607-changing-a-function-dynamically/#findComment-931028 Share on other sites More sharing options...
RichardRotterdam Posted October 5, 2009 Share Posted October 5, 2009 yup first remove the old event then add another. Other then that you can add multiple events to an element Quote Link to comment https://forums.phpfreaks.com/topic/176607-changing-a-function-dynamically/#findComment-931029 Share on other sites More sharing options...
9three Posted October 5, 2009 Author Share Posted October 5, 2009 I tried it right now and my browser crashed. It started going really slow. This is what I have: $('#refresh').unbind('click', refresh()); $('#refresh').bind('click', getGenreSorted()); hm? Quote Link to comment https://forums.phpfreaks.com/topic/176607-changing-a-function-dynamically/#findComment-931040 Share on other sites More sharing options...
RichardRotterdam Posted October 5, 2009 Share Posted October 5, 2009 How many functions do you wish to bind to your element? if it's going to be like an on/off switch you might be better of with a boolean inside one function. Quote Link to comment https://forums.phpfreaks.com/topic/176607-changing-a-function-dynamically/#findComment-931055 Share on other sites More sharing options...
9three Posted October 5, 2009 Author Share Posted October 5, 2009 Heres a scenario: 1. User gets to the page and hits the button "refresh". The button has a function that calls an ajax file to retrieve the latest information, this function is called refresh(). The refresh takes a paramater, place. The place is for where the user is. If the user is in the home page, then I do place = home, so then it translate into refresh('home'). 2. User clicks a letter from "A - Z" that I added. When the user clicks on a letter, the refresh button is dynamically changed to another function. This function is called getGenreSorted(). It takes 1 parameter, which is the letter the user clicked on. If the user clicked on A then it would translate into getGenreSorted('A'). 3. Now the refresh button, when clicked, will refresh content that has an "A" (or whatever letter the user clicked). The refresh button now stores the getGenreSorted() function instead of the refresh() function. What's happening right now is that it crashes at step 2. Hope this has cleared it up some more. Thanks for the help so far. Quote Link to comment https://forums.phpfreaks.com/topic/176607-changing-a-function-dynamically/#findComment-931084 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.