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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:43:53+00:00 2026-05-27T09:43:53+00:00

I am constructing a page which uses jQuery to assign event handlers. Now I

  • 0

I am constructing a page which uses jQuery to assign event handlers. Now I am assigning onclick events to functions but does the page run the script and then construct the HTML or does the page construct HTML and then run the script?

<script type="text/javascript">
    function opinionbyid(popinionid){
        $.post("index.php", {"opinionid":popinionid}, function(data){
            var data2 = JSON.parse(data);
            $(".opinion").html(data2.opinion);
            $(".note").html(data2.note);
        }, "json");
    }
    function loginform() { $(".middle").html($(".loginform").html()); }
    function registerform() { $(".middle").html($(".registerform").html()); }
    function opinionpostform() {};
    $("#loginbutton").click(function(){
        //UNSURE, MAYBE I SHOULD DO IT IN $.ready(function(){ HERE }))
    });
</script>

like

$.ready(function(){ 
    $("#loginbutton").click(function(){
        // code...
    });
})

so far I habitually used script tags in the head section. this is why I think I should use $.ready() because assigning handlers to obects is done after objects are loaded, in jquery’s documentation example.

<!DOCTYPE html>
<html>
<head>
  <style>
  p { color:red; margin:5px; cursor:pointer; }
  p.hilite { background:yellow; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <p>First Paragraph</p>
  <p>Second Paragraph</p>
  <p>Yet one more Paragraph</p>
<script>
    $("p").click(function () { 
      $(this).slideUp(); 
    });
    $("p").hover(function () {
      $(this).addClass("hilite");
    }, function () {
      $(this).removeClass("hilite");
    });
</script>

</body>
</html>

after solution edit:
In the end, I thought “why am I handling the events with script anyway?” now I am using

onClick="dosomething()"

on button properties. no need to complicate anything.

  • 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-05-27T09:43:53+00:00Added an answer on May 27, 2026 at 9:43 am

    HTML pages are parsed from the top down. Parsing JS blocks the parsing of HTML until the JS has been executed:

    <div id="foo">Foo</div>
    <script>
      $('#foo').click(..) //will work because #foo exists in the dom already
      $('#bar').click(..) //wont work because #bar doesn't exist in the dom yet
    </script>
    <div id="bar">Bar</div>
    

    If you’re attaching your scripts just before </body>, then you wont need to wait for document.ready because you’re essentially already there.

    If you’re attaching your scripts within the <head> then you’ll need to wait for document.ready because none of the DOM will have been parsed when the code is executed.

    If you’re making modular code, and you’re not sure where it’ll be added, wait for document.ready before binding so that it’s safe no matter where the script is added.

    If you’re working with dynamic content, or simply don’t want to add a document.ready handler, you can use live (deprecated as of 1.7) or on:

    //pre 1.7
    $('#foo').live('click', ...);
    $('#bar').live('click', ...);
    
    //post 1.7
    $(document).on('click', '#foo', ...);
    $(document).on('click', '#bar', ...);
    

    This format will delegate the event listener to the document object which does exist at the time of execution. jQuery will handle the context behind the scenes to execute everything in the context of the selector provided in the second argument.


    As a side note, make sure to alias jQuery to $ before use if you plan on reusing the code with other libraries. The document.ready aliasing shortcut is one of the best ways to do it:

    jQuery(function ($) {
      //document.ready
    });
    

    Alternatively, use a self-executing closure:

    (function ($) {
      //code here
    }(jQuery));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm constructing an ecommerce application in Symfony, and I have a page which lists
I am constructing a search page with a textbox and a button for now,
I'm constructing a program which uses mprotect() to restrict a block of memory from
I am constructing a pretty massive website that is WP powered , but uses
I'm constructing a simple form in ERB but the HTML produced by the text_field
We are constructing an application that will be interacting with Exchange 2007 calendar functions.
I am constructing a webcomic site, but the chapter order has changed significantly. I
I am constructing a page using Editor Templates and composition. My view model contains
I am creating one construction website in core PHP. It is almost done.But now
I'm creating a website/codeigniter-project that uses views which link to external CSS files. Everywhere

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.