Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Mar 2012
    Location
    US
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Activating functions from other functions?

    Is there a way to activate a function from another function? It has to be in the script tag, it can't be in the HTML section. Can I use something similar to this following code? If not, can anyone give me some help? I have tried to do it various ways, and have looked it up a few times, but to no avail. Can I use something similar to this following code? If not, can anyone give me some help?

    if (condition)
    {activate functionname();}

    Any help I can get would be appreciated. Thanks a lot to anyone who can help.

  • #2
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    Code:
    if (condition){
    functionname();
    }

  • #3
    New Coder
    Join Date
    Mar 2012
    Location
    US
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Does it work like this?

    if (condition)
    {
    alert("Condition is true");
    functionname();
    }

    If not, I can change my code to something that works the same way, but either way, thanks a lot.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    That's how you do it if you want it to run straight away. If you want it to run as a response to an event then you'd attach it to the element in the HTML and the appropriate event handler using

    Code:
    document.getElementById('id_of_tag').oneventname = functionname;
    for example

    Code:
    document.getElementById('myid').onclick= dosomething;
    (or if you want to make sure it doesn't clash with other processing attached to the same event then use an event listener instead)
    Last edited by felgall; 03-16-2012 at 02:12 AM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •