Click here to Skip to main content
15,895,833 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello experts,
I want to pass form as parameter on button click.
Below is what I tried

I cannot say "this" Because this has value "Advanced Search"


I actually want to pass form "TireQuickSearch" id and action and its method in button submit.

  <form id="formNewTireSearch" runat="server">
In this form I have ASP:Dropdown Drop down lists
Cascading drop downs
 </form> 


XML
<form id="TireQuickSearch" action="DealerTireSearchResults.aspx" method="post"  >
      <input type="hidden" name="FormAction" value="TireQuickSearch" />
     <input type="button" name="SearchButton"  value="Advanced Search"  onclick="return ValidateTireQuickSearch(form id="TireQuickSearch" action="DealerTireSearchResults.aspx" method="post");"  />
     </form>



Can anyone help me please
Posted
Updated 11-Mar-15 6:06am
v2
Comments
Sergey Alexandrovich Kryukov 11-Mar-15 11:39am    
Not clear. What's the problem?
—SA
ZurdoDev 11-Mar-15 11:40am    
Why would you pass in the form id?

Pass this to the function, and use the form property[^] to access the parent <form> tag.
JavaScript
function ValidateTireQuickSearch(theButton)
{
    var theForm = theButton.form;
    ...
}

Or, pass this.form to the function:
HTML
<input type="button" name="SearchButton"  value="Advanced Search" onclick="return ValidateTireQuickSearch(this.form);"  />
 
Share this answer
 
Comments
sudevsu 11-Mar-15 12:07pm    
Hey Richard, this doesn't work if we have two forms. It is considering parent form. Instead I want to pass the form where button is in.
can you tell me how to achieve this?
Richard Deeming 11-Mar-15 12:31pm    
The button can only be in a single form. You can't nest forms in HTML.
sudevsu 11-Mar-15 14:57pm    
Worked thank you
Try this:
XML
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("#btnSearch").click(function(){
         var frm = $(this).parent();
        alert(frm.attr('id'));
        alert(frm.attr('action'));
        alert(frm.attr('method'));
    });
});
</script>
</head>
<body>

  <form id="formNewTireSearch" runat="server">
In this form I have ASP:Dropdown Drop down lists
Cascading drop downs
 </form>

<form id="TireQuickSearch" action="DealerTireSearchResults.aspx" method="post"  >
     <input type="hidden" name="FormAction" value="TireQuickSearch" />
     <input type="button" id="btnSearch" name="SearchButton"  value="Advanced Search">
     </form>

</body>
</html>

1. Assign an id to the button, say "btnSearch"
2. In the jQuery, find the parent of this button, that is the form and you can spit out any attribute of this form object.
Read more: jQuery Tutorial[^]
 
Share this answer
 
You can pass anything as a function argument. And in JavaScript, you can pass any argument to any function, even this argument is not declared, the only problem is what this function does. It has nothing to do with jQuery.

As to using "this"… This feature is very important but almost irrelevant to your problem. It's just the implicit function argument passed to a JavaScript function. What it references depends on the context, but it similar but not exactly the same as "this" in traditional OOP languages, in particular, because functions in JavaScript are first-class citizen objects. Please see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this[^].

See also: http://en.wikipedia.org/wiki/First-class_citizen[^].

As to your particular problem, it seems artificial to me; you just need to learn a bit more of general programming and JavaScript programming, not specifically jQuery. If you provide more detail, we can discuss more detailed solutions.

—SA
 
Share this answer
 
v3
<input type="button" name="SearchButton"  value="Advanced Search"  onclick="return ValidateTireQuickSearch(this.form.id, this.form.action);"  />


XML
<script type="text/javascript">
    function ValidateTireQuickSearch(id, action)
    {
        alert (id + " - " + action);
        return false;
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900