Thursday, July 29, 2010

Javascript: How to Find the enclosing/parent FORM Element

I was working on a project and came across a situation where we have more than one forms being generated dynamically in one page. And each form contains lots of input fields. To identify the enclosing/parent form element on some action of input tags, I used the following code:

Put these two javascript functions in script tag under head section of your page:


function findParentForm(elem){
var parent = elem.parentNode;
if(parent && parent.tagName != 'FORM'){
parent = findParentForm(parent);
}
return parent;
}

function getParentForm( elem )
{
var parentForm = findParentForm(elem);
if(parentForm){
alert("Form found: ID = " + parentForm.id + " & Name = " +parentForm.name);
}else{
alert("unable to locate parent Form");
}

}




Here is the example to call this code:


<body>
<h3> Form 1 </h3>
<form name ="formName1" id ="formId1">
<span>
<div>
<input type ="button" name ="button1" value="button1" onclick="javascript:getParentForm(this);"/> <br/>
</div>
</span>

</form>
<hr/>
<h3> Form 2 </h3>
<form name ="FormName2" id ="formId2">
<span>
<div id ="asda" name ="divName">
<input type ="button" name ="button1" value="button2" onclick="javascript:getParentForm(this);"/> <br/>
</div>
</span>
</form>
</body>


18 comments:

Unknown said...

or just use this.form :)

Praylin S said...

Hi! That's an awesome blog. Recently I completed Javascript Training in Chennai and I am doing a project now. I came across a similar situation like yours. Thanks for sharing. This'll really help me out.
Javascript Training Courses | Javascript Training | Javascript Training Institute in Chennai

Aliya Manasa said...

I was recommended this web site by means of my cousin. I am now not certain whether this post is written through him as nobody else recognise such precise about my difficulty. You're amazing! Thank you!
python Training in Pune
python Training in Chennai
python Training in Bangalore

Anbarasan14 said...

Reading your blog left me with some useful info and this was an awesome blog.

Spoken English Classes in Chennai
Spoken English Class in Chennai
Spoken English in Chennai
Best Spoken English Classes in Chennai
Best Spoken English Institute in Chennai

priya said...

Well you use a hard way for publishing, you could find much easier one!
Data Science Training in Chennai
Data Science training in kalyan nagar
Data science training in Bangalore
Data Science training in marathahalli
Data Science interview questions and answers
Data science training in bangalore

Keerthana said...

Articles are inspiring,Great Information's are shared here.I look forward to your writing
python training in chennai | python training in annanagar | python training in omr | python training in porur | python training in tambaram | python training in velachery

Aruna said...

Appreciated For the Persistence of your Work...Keep Sharing
Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

aarthi said...

The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
Java training in Chennai

Java training in Bangalore

Java training in Hyderabad

Java Training in Coimbatore

Java Online Training

EXCELR said...

The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.data science course in HyderabadThe strategy you have posted on this technology helped me to get into the next level and had lot of information in it.data science course in Hyderabad

Jayalakshmi said...

I really have to search sites with relevant information on given topic and provide them to teacher our opinion and the article.thanks a lot,
data science training in chennai

data science training in tambaram

android training in chennai

android training in tambaram

devops training in chennai

devops training in tambaram

artificial intelligence training in chennai

artificial intelligence training in tambaram

shiny said...

Thanks for sharing this unique information with us. Your post is really awesome. Your blog is really helpful for me..


angular js training in chennai

angular js training in annanagar

full stack training in chennai

full stack training in annanagar

php training in chennai

php training in annanagar

photoshop training in chennai

photoshop training in annanagar

deiva said...

Reading your blog left me with some useful info and this was an awesome blog.
web designing training in chennai

web designing training in omr

digital marketing training in chennai

digital marketing training in omr

rpa training in chennai

rpa training in omr

tally training in chennai

tally training in omr

data scientist course said...

Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing.
data scientist training and placement in hyderabad

David Fincher said...

This post is so interactive and informative.keep update more information...
RPA Training in Tambaram
RPA Training in Chennai

360DigiTMG said...

Such a helpful article. Interesting to peruse this article.I might want to thank you for the endeavors you had made for composing this wonderful article.
best data science training in hyderabad

Ramesh Sampangi said...

Nice blog and informative content. Really useful for many people, I bookmarked your website for further blogs. Thanks, you.
Data Science Course with Placements in Hyderabad

Pavithra Devi said...

This post is so interactive and informative.keep update more information…
SEO Training in Anna Nagar
SEO Training in Chennai

queenarts said...

Very informative blog thanks sharing with us
Building Contractors in Chennai