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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:05:42+00:00 2026-06-05T00:05:42+00:00

html: <div id=search> <input id=term type=text value=enter your search /> <button id=hit type=button name=>Search</button>

  • 0

html:

<div id="search">
  <input id="term" type="text" value="enter your search" />
  <button id="hit" type="button" name="">Search</button>
</div>

jQuery:

$(document).ready(function() {
  var term = $('#term').val();
  $('#hit').click(function() {
    alert(term);
  });
});

The problem is that , no matter what I type in the input field, then hit the button, it always alert the original input value which is “enter your search”.

How can I fix it?

  • 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-05T00:05:44+00:00Added an answer on June 5, 2026 at 12:05 am

    The problem you’re having is that this whole block of code gets executed on the DOM ready event.

    var term = $('#term').val(); is being evaluated only once and storing ‘enter your search’ in the term variable. This is why no matter what you change the value to, the variable still holds the initial value when the page was rendered.

    Instead what you should do is something more like the following:

    JQuery

    $(document).ready(function() {
      $('#hit').click(function() {
        alert($('#term').val());
      });
    });
    

    In this bit of code, the value of the element with id term is evaluated when the click event listener fires.

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

Sidebar

Related Questions

html: <div id=wrapper> <div id=header> <div id=logo> </div> <div id=search> <input type=text name=match id=match/>
here is link to an example: http://techchorus.net/demos/jquery/hiding-input-elements-in-a-div.html It actally disables few elements on click
the html code: <div class=fr_search> <select class=search_l onchange=selectSearch(select)> <option value=0>whole site search</option> <option value=1>google
Given: Url - http://www.contoso.com/search.php?q= {param} returns: -html- --body- {...} ---div id='foo'- ----div id='page1'/- ----div
I have below html code in one of the opensource project. <form action=/wiki/bin/view/Main/Search> <div
I have a jQuery search that gets a search term from the user and
i'm trying to send the value of the search textbox via jquery on keydown
I have modified the dataTables filter input into a Jquery Mobile search filter ,
I have a text box and a button, which is described with the HTML/CSS
I have this code var file = getUrlVars()[file]; if(file){ document.getElementById('search').value = file; } function

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.