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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:51:19+00:00 2026-05-14T19:51:19+00:00

inside a jquery method (on a click event of a button) i have the

  • 0

inside a jquery method (on a click event of a button) i have the following code:

    $(selector).html("<form action='/Tracker/CopyFood' method='post'><input type='hidden' name='Id' value=" + id + "><input class='very-small-input' type='text' name='Quantity' value='1' /> for <select name='Date'><option value='Today'>Today</option><option value='Yesterday'>Yesterday</option></select><select name='Meal'><option value='Breakfast'>Breakfast</option><option value='Lunch'>Lunch</option><option value='Dinner'>Dinner</option><option value='Evening Snack'>Evening Snack</option></select><input type='button' class='submitCopyFood' value='Add'> or <div style='display:inline' class='CancelCopy'>Cancel</div></form> ");

it seems to work on the screen, but the button click “submitCopyFood” the event was firing but it wasn’t submitting the form. When i click “View Selection Source” in firefox, i see the issue. All i see is this:

   <input name="Id" value="128" type="hidden"><input class="very-small-input" name="Quantity" value="1" type="text"> for <select name="Date"><option value="Today">Today</option><option value="Yesterday">Yesterday</option></select><select name="Meal"><option value="Breakfast">Breakfast</option><option value="Lunch">Lunch</option><option value="Dinner">Dinner</option><option value="Evening Snack">Evening Snack</option></select><input class="submitCopyFood" value="Add" type="button">  or <div style="display: inline;" class="CancelCopy">Cancel</div>

Its like i never added the form anywhere. Is there any reason why i dont see the:

<form action='/Tracker/CopyFood' method='post'>

or the

</form>

in the source code inside the selected source. Is there something special that i need to do here?

Updated:

here is the button click event. To be clear, this event fires fine but this line fails below:

var myForm = parentDiv.children('form');

because myForm is empty (as there is no form element)

Here is the full click event:

$(document).ready(function() {
    $('.submitCopyFood').live('click', function() {

    debugger;

    var parentDiv = $(this).parent('.copyFoodInstance');
    var myForm = parentDiv.children('form');

    $.post(myForm.attr('action'), myForm.serialize(), function(data) {
        debugger;
        parentDiv.attr("fromInner", "1");
        parentDiv.attr("myset", "0");
        parentDiv.html("<img  BORDER=0 src='../../images/copy1.png' />");

    });
});

});

  • 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-05-14T19:51:19+00:00Added an answer on May 14, 2026 at 7:51 pm

    If you are adding the form inside another form, that is invalid. The browser will try to correct the code (in different ways depending on the browser), one way is to remove the form tag.

    If you want the button to do something when you click it, you have to set an onclick event for it. If you want the button to submit the form, you should use an input with type="submit" instead.

    Update:

    Are you really really sure that there isn’t a form surrounding the element where you try to add the form? I can easily replicate the exact behaviour by putting a form around the element where the form is added. If the outer form isn’t there, the form is added just fine and shows up when viewing the selection source.

    The parent method only goes one level, so the parentDiv variable is a jQuery object with zero elements as the parent element of the button is the form. You would want to use the parents or closest method to find the element.

    Working example:

    <html>
    <head>
    <title></title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <script type="text/javascript" src="jquery-1.4.1.js"></script>
    
    <script type="text/javascript">
    
    $(function(){
    
      var id = 'asdf';
      $('.copyFoodInstance').html("<form action='/Tracker/CopyFood' method='post'><input type='hidden' name='Id' value=" + id + "><input class='very-small-input' type='text' name='Quantity' value='1' /> for <select name='Date'><option value='Today'>Today</option><option value='Yesterday'>Yesterday</option></select><select name='Meal'><option value='Breakfast'>Breakfast</option><option value='Lunch'>Lunch</option><option value='Dinner'>Dinner</option><option value='Evening Snack'>Evening Snack</option></select><input type='button' class='submitCopyFood' value='Add'> or <div style='display:inline' class='CancelCopy'>Cancel</div></form> ");
    
      $('.submitCopyFood').live('click', function() {
    
        var parentDiv = $(this).closest('.copyFoodInstance');
        var myForm = parentDiv.children('form');
    
        alert(parentDiv.length); // shows 1
        alert(myForm.length); // shows 1
    
      });
    
    });
    
    </script>
    
    </head>
    <body>
    
    <div class="copyFoodInstance"></div>
    
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to manipulate the HTML inside an iframe using jQuery. I thought
I have this bit of javascript written with jQuery 1.2.5. It's contained inside the
Inside my page, I have the following: <aspe:UpdatePanel runat=server ID=updatePanel> <ContentTemplate> <local:KeywordSelector runat=server ID=ksKeywords
I am using jQuery and JavaScript, I need to call the jQuery function inside
I'm using jQuery to wire up some mouseover effects on elements that are inside
Inside my code I'm generating hashes of URLs, (which are practically of unbounded length).
Inside a service, what is the best way to determine a special folder path
Inside VS2005, our whole programming staff gets this error message sporadically and it is
Inside a windows batch file I'd like to figure out what the fully qualified
Working inside the context of an ASP.NET application I am creating a page that

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.