Solved

get form name

Posted on 2011-02-27
9
273 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
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
 
LVL 25

Author Comment

by:dgrafx
ID: 34991969
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now