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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:19:38+00:00 2026-06-14T05:19:38+00:00

I have the following code to put helper text in a search input box

  • 0

I have the following code to put helper text in a search input box that is removed when you click in the box and returned if you click anywhere else without changing it:

$('#search-form input[type="text"]').each(function(){
    var defaultVal = 'Category Search';
    $(this).focus(function(){
      if ($(this).val() == defaultVal){
        $(this).removeClass('active').val('').css('color', '#000');;
      }
    })
    .blur(function(){
      if ($(this).val() == ''){
        $(this).addClass('active').val(defaultVal).css('color', '#CCC');
      }
    })
    .blur().addClass('active');
});

But if the user clicks submit without clicking in the input box, the site will search for the helper value (Category Search).

What I need to do is on submit check the value and if it’s Category Search then do the following things:

  • Change helper text to Please enter a category
  • Don’t submit the form.

I tried this but it stopped my default value (Category Search) from working all together:

$('#search-form input[type="submit"]').click(function() {
    if ($(this).val() == "Category Search")
        //do stuff here
});​
  • 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-14T05:19:40+00:00Added an answer on June 14, 2026 at 5:19 am

    jsBin demo

    var defaultVal = 'Category Search';
    var enterCat = 'Please enter a category';
    
    $('#search-form input[type="text"]').each(function(){
      
        $(this).focus(function(){
          if ($(this).val() == defaultVal || $(this).val() == enterCat){
            $(this).removeClass('active').val('').css('color', '#000');
          }
        })
        .blur(function(){
          if ($.trim($(this).val()) === ''){
            $(this).addClass('active').val(defaultVal).css('color', '#CCC');
          }
        })
        .blur().addClass('active');
    });
    
    $('#search-form').submit(function(e) {
      
      if ( $(this).find('[type="text"]').val() === defaultVal){
        e.preventDefault();
        $(this).find('[type="text"]').prop('value', enterCat);    
      } 
      
    });
    

    and use also $.trim() (whitespaces) before searching for blank inputs 🙂

    • 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: payoffs2exchanges.put(point, exchange); if (!payoffs2exchanges.containsKey(point) ) { game.log.fine(yes); } else
I have the following code which should put programs startable in Bash. if [
I have the following code which works when i put it in any blank
I have following code that I am compiling in a .NET 4.0 project namespace
I have the following code: inside i put 2 alert() for debug. why does
I have the following code: if(!partialHits.get(req_nr).containsKey(z) || partialHits.get(req_nr).get(z) < tmpmap.get(z)){ partialHits.get(z).put(z, tmpmap.get(z)); } where
All right so I have the following code and all it does is put
I have the following code... var min_val = [{id:driverDOA,value:date_of_accident},etc etc...];//put validation elements in an
I have the following code to put all my users from a multichoice field
I have the following code that populates two drop down lists, it all works

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.