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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:10:35+00:00 2026-06-15T18:10:35+00:00

I have two jQuery codes – http://jsfiddle.net/Lijo/CXGX7/7/ and http://jsfiddle.net/Lijo/CXGX7/8/ . The first code returns

  • 0

I have two jQuery codes – http://jsfiddle.net/Lijo/CXGX7/7/ and http://jsfiddle.net/Lijo/CXGX7/8/ . The first code returns undefined whereas the second code returns text of button.

QUESTIONS

  1. What is the reason for this difference in result?
  2. Why is the first code not returning expected text of button?

Note: I verified that both are using same version of jQuery (by an alert of jQuery)

alert($.fn.jquery);

CODE

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>

</title>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.1.js"></script>

<script type="text/javascript">

    alert($('.myButton').attr("value"));

</script>

</head>
<body>

<form method="post" action="Test.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE0MDM4MzYxMjNkZMycQvsYQ+GPFsQHoQ8j/8vEo2vQbqkhfgPc60kxXaQO" />
</div>

<div class="aspNetHidden">

<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAwKqxqqrCgLi/JazDQKM54rGBqgaroRQTXJkD1LyUlVxAmLRCNfTGVe73swQBMemBtvN" />
</div>
<div>

    <input name="txtEmpName" type="text" id="txtEmpName" />

    <input type="submit" name="Button1" value="Submit" id="Button1" class="myButton" />
</div>
</form>
</body>
</html>

REFERENCES

  1. Retrieve Button value with jQuery
  2. How to determine and print jQuery version?
  • 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-15T18:10:37+00:00Added an answer on June 15, 2026 at 6:10 pm

    That’s because in the first version, when your script executes, the button is probably not yet loaded in the DOM.

    In this version, you are writing alert directly in the html page. So, when that portion of the page is loaded in the browser, it will be executed immediately. At this point, the rest of your page is not loaded, so your $('.myButton') selector will return nothing (you can check it by $('.myButton').length).

    In this version, you are writing this code inside the JavaScript panel of JsFiddle. The JS code that you write in here runs inside an onload event handler, which ensures that all of your html has been loaded before executing any JS code inside it.

    So, in the first version, your code is converted to something like this –

    $(document).ready(function () {
        alert($.fn.jquery);
    });
    

    and for the second version –

    $(document).ready(function () {
        alert($('.myButton').attr("value"));
        alert($.fn.jquery);
    });
    

    Edit

    The $(document).ready() function is called whenever DOM is fully loaded in the browser. You pass a callback function as an argument to this function which will be called when it happens. Usually, anything you want to do with jQuery, you do it inside that callback. This is the very first thing to learn about jQuery. You can get an official tutorial about it here.

    This method is somewhat similar to window.onload, except that it fires as soon as your HTML document has been loaded, and window.onload is called after all the contents of a page(including images and other resources) has been fully loaded.

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

Sidebar

Related Questions

Code example: http://jsfiddle.net/MhEPw/1/ I have two jQuery Deferred objects. I want to have more
I have two variables in a jquery code and if item.a not exist, it
I have two bits of code: a form and a jQuery function. My problem
I have my own simple equal heights code in jQuery to make two columns
I have two jQuery functions. The first one below I want to run first,
Assuming I have the following two JQuery functions - The first, which works: $(#myLink_931).click(function
I am new to ASP.NET and JQuery. Currently, I have the codes for JQuery
I have two jQuery autocomplete textboxes on a mvc web page. One that returns
I have two pages - a simple HTML code example page from a jQuery
I have two pieces of jquery code, they work perfectly separate, but together -

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.