Solved

get form name

Posted on 2011-02-27
9
274 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JS Plugin 4 29
Basic JavaScript Question 3 36
Use a select control to call a javascript function 5 29
Rhino javascript replace special characters 4 17
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 …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

948 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

20 Experts available now in Live!

Get 1:1 Help Now