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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:44:00+00:00 2026-06-13T05:44:00+00:00

This is an excerpt of my javascript file: $(function() { if ($(‘form#search’).length > 0)

  • 0

This is an excerpt of my javascript file:

$(function() {
    if ($('form#search').length > 0) {
        setSearchForm();
    }
});

function setSearchForm() {
    var text=$('form#search input[type=text]');
    if (text.val().length != 0) {
        text.next('input[type=submit]').removeProp('disabled');
    } else {
        text.next('input[type=submit]').prop('disabled', true);
    }
}

My problem is: All of my subpages use this .js file but not all subpages do have a search form.

So on document ready I check for the existence of a search form and then call setSearchForm() only if a search form exists.

I found that this is the only way to prevent setSearchForm() from running on every page. Because if there is no search form and the function gets triggered anyway, none of the other functions I have in my js file will work anymore.

Is there a more elegant solution to this, e.g. by rewriting the function somehow?

I am fairly new to jQuery, so the solution is probably a simple one!

Thanks for any help.

  • 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-13T05:44:01+00:00Added an answer on June 13, 2026 at 5:44 am

    Put the two functions in a separate .js file and import it only on pages that have a search form.

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

Sidebar

Related Questions

I have a jquery script in my file that says this: <script type=text/javascript> $(document).ready(function(e){
Greetings! I must be seeing things. Look at this excerpt from the iPhone OS
This is an excerpt of some c++ code, that i'll have to explain in
I have this HTML structure (excerpt): <td> <select id=test1 class=pub_chooser> <option value=99>All Publications</option> <option
I read this question in stackoverflow. The excerpt answer provided by bbum is below:
I have an issue where the JavaScript source file is loading in popup for
This question regards my personal photography website. I use the following javascript to generate
I had used jquery media plugin to display pdf file in html This is
I have the following javascript function : function addNewUpload() { if(formNumber == 1){ displayRemove();
I am unable to load an XML file into a JavaScript Document in Google

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.