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 7 of 7
  1. #1
    Regular Coder Mooseman's Avatar
    Join Date
    Sep 2010
    Posts
    123
    Thanks
    7
    Thanked 3 Times in 3 Posts

    Question jQuery function with other variable?

    Hello,
    I'm trying to write a function where jQuery is used, and I need a variable from outside. How do I do this? Thanks!

  • #2
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    im not sure what you mean so:


    Code:
    var a ="hello world";
    
    function myfunc(){
    alert(a);
    }
    myfunc();

  • #3
    Regular Coder Mooseman's Avatar
    Join Date
    Sep 2010
    Posts
    123
    Thanks
    7
    Thanked 3 Times in 3 Posts

    Question

    Quote Originally Posted by DanInMa View Post
    im not sure what you mean so:


    Code:
    var a ="hello world";
    
    function myfunc(){
    alert(a);
    }
    myfunc();
    That's about what I have now. What I need is for jQuery code to run inside the function.

  • #4
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    uhm, you just put it inside the function....
    Maybe you shouldexplain what you are trying to accomplish instead?

  • #5
    Regular Coder Mooseman's Avatar
    Join Date
    Sep 2010
    Posts
    123
    Thanks
    7
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by DanInMa View Post
    Maybe you shouldexplain what you are trying to accomplish instead?
    I'm going to post my code: (Some variable values edited)

    Code:
    function myFunction(num){
         if(num > 10){
              $("#elementtohide").hide();
         }
    }
    $("#clickable").click(function() {
         var num = "Hello World"
         myFunction(num);
    });
    When I use the above, the jQuery function does not run. An alert will work, though. Thanks!

  • #6
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    Code:
    var num= 1;
    $("#clickable").click(function() {
         if(num > 10){
              $("#elementtohide").hide();
         }
    });
    or


    Code:
     var num = 0;
    function myFunction(num){
         if(num > 10){
              $("#elementtohide").hide();
         }
    }
    $("#clickable").click(function() {
         myFunction(num);
    });

  • #7
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    I would also like to point out that the click function will nto work if you are simply adding it to your head section.

    it should be declared in a $(document).ready(function(){

    })

    block in the head, or it should be at the bottom of your html


  •  

    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
    •