Solved

get form name

Posted on 2011-02-27
9
278 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 500 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

740 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