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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:47:14+00:00 2026-06-16T16:47:14+00:00

I am trying to get jQuery to submit one of two forms when a

  • 0

I am trying to get jQuery to submit one of two forms when a submit_tag is clicked. I am basing my conditions on the presence of text, so whichever form has text in then submit that one. My forms looks like this

<div class="container margin50">
 <div class="row">
  <div class="span6 offset3 cf formBackground">
   <h1>CoverArt Finder</h1>

   <h3>Search Movies</h3>
   <%= form_tag main_results_path, :method => "get", :id => 'submitMovie' %>
   <%= text_field_tag 'search', nil, :placeholder => 'Enter Film Name Here.....', :id => 'movieForm' %>

   <h1>OR<h1>

   <h3>Search Albums</h3>
   <%= form_tag album_album_results_path, :method => "get", :id => 'submitAlbum' %>
   <%= text_field_tag 'search', nil, :placeholder => 'Enter Artist Name here.....', :id => 'albumForm' %>
   <%= submit_tag "search", :id => 'submitForm' %>

   </div>
  </div>
 </div>

HTML

<div class="container margin50">
 <div class="row">
  <div class="span6 offset3 cf formBackground">
   <h1>CoverArt Finder</h1>
   <h3>Search Movies</h3>
    <form id="submitMovie" method="get" action="/main/results" accept-charset="UTF-8">
     <input id="movieForm" type="text" placeholder="Enter Film Name Here....." name="search">
    <h1>OR</h1>

    <h3>Search Albums</h3>
    <input id="albumForm" type="text" placeholder="Enter Artist Name here....." name="search">
    <input id="submitForm" type="submit" value="search" name="commit">
    </form>
    </div>
   </div>
  </div>

I also want to show an error message if both are filled in. Another question here is: does the text event count the placeholder as text? If so, the text event won’t work, will it?

So far I have come up with this, which I know is wrong; would someone point me in the right direction?

$(document).ready(function() {
 $('#submitForm').click(function(e) {
   e.preventDefault();
    if ($('#submitMovie').text().length > 0) 
    $('#submitMovie').submit();
   else if
   ($('#submitAlbum').text().length > 0)
   $('#submitAlbum').submit();
   else
    ($('#submitAlbum' + 'submitMovie').text().length > 0)
   alert("Cant fill in both forms");

  });
 });

Also I have just noticed from posting my HTML is that the form id for albumSearch is not present. Any ideas why this would be happening?

  • 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-16T16:47:15+00:00Added an answer on June 16, 2026 at 4:47 pm

    You should check for the presence of text in your inputs, not in your forms.

    Fiddle: http://jsfiddle.net/rdqch/1/

    $('#submitForm').click(function(e) {
    
        var movie = false,
            album = false,
            $movieForm = $('#movieForm'),
            $albumForm = $('#albumForm');
    
        // Prevent the default action of the submit button
        e.preventDefault();
    
        if ($movieForm.val().length) {
            movie = true;
        }
    
        if ($albumForm.val().length) {
            album = true;
        }
    
        if (movie && album) {
            alert('Can\'t fill in both forms.');
        } else if (movie) {
            $movieForm.closest('form').submit();
        } else if (album) {
            $albumForm.closest('form').submit();
        }
    
    });
    
    // This is just for demonstration   
    $('#submitMovie').on('submit', function (event) {
        event.preventDefault();
        alert('We have submitted a movie search');
    });
    
    $('#submitAlbum').on('submit', function (event) {
        event.preventDefault();
        alert('We have submitted an album search');
    });
    

    One note: I know it’s beyond the scope of your question and you may have already been ahead of me, but there’s no reason to tell your user they can’t use both forms if you could just make the interaction fix itself automatically. Just have any typing into one of the search boxes erase anything currently inside the other one.

    Example: http://jsfiddle.net/rdqch/2/

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

Sidebar

Related Questions

I'm trying to get the value of a form field on submit using jQuery.
I'm trying to get twitter bootstrap's .popover working within a .submit jquery event but
I am trying to get jquery's .ajax() to send values sent to a form
I have two columns, each with many rows/divs. I'm trying to get jQuery UI
I'm trying to submit a form with AJAX using jquery: <form name=myform action=> <table
I am trying to submit a form by using jquery automatically after page load
I have some problems when trying to get multiple fields from a jQuery form.
I am trying to submit a form using JQuery to a PHP page. But
I am trying to submit a form with jQuery and I must be missing
I am trying to get jquery validate to work on multiple fields. Reason being

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.