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 8456551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:34:22+00:00 2026-06-10T12:34:22+00:00

I have following code that works – $(function(){ $(‘#testbutton’).click(function(){ var checkedValue = $(‘[name=someRadioGroup]:radio:checked’).val(); $(‘#result’).html(‘The

  • 0

I have following code that works –

$(function(){
    $('#testbutton').click(function(){
        var checkedValue = $('[name="someRadioGroup"]:radio:checked').val();
        $('#result').html('The radio element with value <tt>' + checkedValue + '</tt> is checked');
    });
});

I understand that $(‘#testbutton’) fetches the HTML element with id testbutton and assigns an event handler to it.

But I don’t understand why I need to put that code inside a function literal?

If I remove the $(function(){ (and the corresponding }); ) it does not work.

However other code can work without being wrapped in a function literal. Example –
alert('Hi there')

What is the difference? Why does alert work but the event assignment statement does not?

  • 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-10T12:34:24+00:00Added an answer on June 10, 2026 at 12:34 pm

    It is not a requirement.

    You are putting your code in an event handler that is invoked when the DOM is ready.

    If you don’t put your code in a DOM ready handler, and if your code performs DOM selection, you need to find some other way to to make sure the DOM is ready before it runs.

    If your code doesn’t perform DOM selection, then it’s not needed. That’s why your alert() works, because it doesn’t need to fetch any elements.

    An alternative is this.

    <body>
       <button id="testbutton">click</button>
       <div id="result"></div>
    
       <script type="text/javascript" src="/your/script.js"></script>
    </body>
    

    This places your code below all the elements on the page, so they are certain to be available when your code runs.

    Another alternative is to use a window.onload handler.

    window.onload = function() {
        // your code
    };
    

    or using jQuery.

    $(window).on("load", function() {
        // your code
    });
    

    This is similar to the DOM ready handler, except that it waits for all resource, like images, to be loaded.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that works on Firefox and Chrome $("#adicionarItem").click(function(){ $.ajax({ type:
I have the following code that works great for row click, but I want
I have the following Javascript code that works perfectly: $(document).ready(function() { $(#Select1).setDefault(); $(#Select2).setDefault(); $(#Select3).setDefault();
I have the following code that works: @for (var index = 0; index <
I have the following code that works inside a function: $('#MyPanel').stop().animate({ top: PanelTop },
I have the following code that works: <script type=text/javascript> $(document).ready(function() { // Initialise the
I have the following code that works: <script type=text/javascript> $(document).ready(function() { // Initialise the
I have the following jQuery code that works: $(document).ready(function () { $(input[id^='pos_']).change(UpdatePosition) }); function
I have the following code that works fine up to the last line in
I have the following code that works without using throw however when I use

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.