[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

get form name

Posted on 2011-02-27
9
Medium Priority
?
282 Views
Last Modified: 2012-05-11
how can I get form name or actually the form object would be fine - either one - from a link?
for example : <a href="" onclick="alert(the name of the form this link resides in); return false;">Click</a>

thanks
0
Comment
Question by:dgrafx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 34991868
Using : document.forms[0]
<a href="#" onclick="alert(document.forms[0].name); return false;">Click</a>

Open in new window


To get the first one (or the only one) : document.getElementsByTagName("form")[0]
<a href="#" onclick="alert(document.getElementsByTagName('form')[0].name); return false;">Click</a>

Open in new window

0
 
LVL 20

Accepted Solution

by:
Proculopsis earned 2000 total points
ID: 34991887

...it's not quite that simple, try this instead:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_26850984.html</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript"> 

jQuery(document).ready(function() {

  $(".click-me").click( click );

});

function click() {
  var formName = $(this).closest("form").attr("name");
  alert( formName );
}

</script>

</head>
<body>

<form name="form-one">
  <a href="#" class="click-me">Click</a>
</form>

<form name="form-two">
  <a href="#" class="click-me">Click</a>
</form>


</body>
</html>

Open in new window

0
 
LVL 25

Author Comment

by:dgrafx
ID: 34991956
closest ??? !!!
never heard of it - if it works - awesome!!!
not doubting you - I just can't test it till tomorrow morning ...

thanks - will be in touch ...
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 25

Author Comment

by:dgrafx
ID: 34991969
0
 
LVL 25

Author Closing Comment

by:dgrafx
ID: 34996727
you the man ...
0
 
LVL 25

Author Comment

by:dgrafx
ID: 34997094
one final question on this post if youd be so kind ...
You showed me how to get form name or id - which is what I need some of the time.
Other times I need the form object - ya know like if you had a form named myForm then what I mean is document.myForm will give you the parent form object.

how can i get that?
thanks ...
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 34997236
Just remove the .attr()

var formName = $(this).closest("form").attr("name");
var formObject = $(this).closest("form");
0
 
LVL 25

Author Comment

by:dgrafx
ID: 34997276
hey thanks buddy!
I should have opened a new question - sorry ...
We'll get ya next time!
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 34997633
No need. Thanks anyway :)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question