/    Sign up×
Community /Pin to ProfileBookmark

how to pass div id to a function

Hi all,
i’m in this situation, i’ve this html+javascript block:

<div id=”theID”>
<form action=”javascript:MyFunction()”>
….
</form>
</div>

How can i take the id=”theID” and pass it to MyFunction()?
I don’t know in advance the id value.

Thank you!

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@mishkinAug 31.2006 — well you'll need to have your <div> held as a variable first of all. It's a little tricky getting it into a variable if you don't know the ID. If you know (for example) that the <div> is always the third <div> on the page, you can use

MyDiv = document.getElementsByTagName("div").item(2) //will get the third <div> down

then just do MyDivId = MyDiv.getAttribute("id")
Copy linkTweet thisAlerts:
@francescoauthorAug 31.2006 — Thanks...i' know that there is always the div tag with the id before a form..but there isn't a way for "walking back" from the form to the div?

For Example:

javascript:MyFunction( currentPosition.back.back.div.id )
Copy linkTweet thisAlerts:
@KorAug 31.2006 — Somehow I would say your code is not correct from the begining. Javascript functions are to be call on [i]events[/i], or [B]action[/B] is not an [I]event[/I], it is a [I]target[/I]. So that the code shoud be

<form action="" onsubmit="return MyFunction([B]this[/B])">

And your function should set the action:

function MyFunction(f){

[COLOR=DarkGreen]......[/COLOR]

f.action='whichever.php';

}

Now, comming back to your problem... Yes, it is easy to retreive the object's parent on using DOM reference [B]parentNode[/B]

function MyFunction(f){

[COLOR=Blue]var divID = f.parentNode.id;[/COLOR]

[COLOR=DarkGreen]...... do whichever u want with the variable divID......[/COLOR]

f.action='whichever.php';

}
Copy linkTweet thisAlerts:
@francescoauthorSep 01.2006 — Ok, but in this way when i use the "f.parentNode.id" i've the error in the javascript Console:

f has no properties
Copy linkTweet thisAlerts:
@KorSep 01.2006 — I get no error in IE nor in FF
<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;meta http-equiv="Content-Style-Type" content="text/css"&gt;
&lt;meta http-equiv="Content-Script-Type" content="text/javascript"&gt;
&lt;script type="text/javascript"&gt;
function MyFunction(f){
alert(f.parentNode.id)
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id="foo"&gt;
&lt;form action="" onsubmit="return MyFunction(this)"&gt;
&lt;input type="submit" value="GO" name="sub"&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

There must be other error in your code.
×

Success!

Help @francesco spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...