Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8043591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:57:07+00:00 2026-06-05T04:57:07+00:00

Can you do this? var myFunction = function(){ $(function(){ // jQuery code here $(#myField).hide();

  • 0

Can you do this?

var myFunction = function(){
    $(function(){
        // jQuery code here
        $("#myField").hide();
    })    
}();

I had some code like this which worked fine almost everywhere but then I discovered it only worked about 80% of the time in FF 9.0.1. The fix was to do this:

var myFunction = function(){
    $(function(){
        // jQuery code here
        $("#myField").hide();
    })    
};
myFunction();

Now I’m a bit puzzled why this was necessary

EDIT:
I can see this is a bit confusing. Maybe what I should have done would have been this instead:

var myFunction = function(){ 
    // jQuery code here
    $("#myField").hide();       
};

$(function(){
    myFunction();
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T04:57:08+00:00Added an answer on June 5, 2026 at 4:57 am

    The problem is that you are not correctly defining the function and then putting jQuery code, when the JavaScript parser reads you code it will ignore the var definition and execute the jQuery that you defined on document ready. In case you don’t know:

     $(function(){})
     // is equivalent to 
     $(document).ready(function(){}). 
    

    The best option for you would be to create a function like:

     function myFunction(){  
    
        $("#myField").hide();
    
     }
    
     $(function(){
    
        //after whenever you want it do 
        myFunction();
    
     })    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can do this jQuery.fn.validate = function(options) { var defaults = { validateOPtions1 :
I have a jquery function like this: (function($){ $.fn.myjqfunction = function(cfg){ var foo1, foo2;
In Actionscript you can have global variables like this: var number : Number =
If I write something like this: var img = $(new Image()).attr('src', image.src); How can
can i use custom attributes with linkbutton? like this: var linkb= new LinkButton(); linkb.ID
You can backreference like this in JavaScript: var str = 123 $test 123; str
How can I dynamically name id's using javascript? Something like this: js: var idName
In this simplistic example: var myFunction = function(obj) { console.log($(obj)); } I am using
I have a function like this, that I would like to refactor function Myfunction(sUrl,
I can do this var v = new class_name() { media_id = m, fn

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.