Page 1 of 1
how to hide element
Posted: Fri Sep 21, 2007 3:58 pm
by yacahuma
Hello,
I need a javascript that given the id(or name) of an element return me it's 'p' parent
ex
Code: Select all
<p><label for="x1"><input name="x1"></p>
<p><label for="x2"><input name="x2"></p>
function getParentOf("x1")
{
//
}
Why?
I want to hide and show different elements of the form.
I could assign and id to each 'p' and that works but I thought it will be nicer if I dont have to give a name to every 'p' element
Posted: Fri Sep 21, 2007 4:21 pm
by pickle
Look up Javascript DOM scripting.
While you could probably find it in 5 minutes if you bothered to check google, I'm feeling generous - what you're looking for is something like:
Code: Select all
document.getElementById('x1').parent;
thank you
Posted: Fri Sep 21, 2007 5:47 pm
by yacahuma
Well I was not sure what I was looking for + I feel better asking it here.
Thank you for the answer.
Posted: Sat Sep 22, 2007 9:36 am
by superdezign
parentNode.
i get an error
Posted: Sun Sep 23, 2007 8:04 pm
by yacahuma
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I get an error on this. document.getElementById(id) has no properties
[syntax="html"]
<html>
<head>
<title>Page title</title>
<script type="text/javascript" src="calendarDateInput.js">
/***********************************************
* Jason's Date Input Calendar- By Jason Moon http://calendar.moonscript.com/dateinput.cfm
* Script featured on and available at http://www.dynamicdrive.com
* Keep this notice intact for use.
***********************************************/
</script>
<script type="text/javascript">
<!--
function toggle_visibility(id) {
var e = document.getElementById(id).parentNode;
if(e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'block';
}
//-->
</script>
</head>
<body>
<a href="#" onclick="toggle_visibility('orderdate');">toggle</a>
<form>
<p><label for="orderdate">Date:</label><script>DateInput('orderdate', true, 'DD-MON-YYYY')</script></p>
</form>
</body>
</html>
feyd | Please use[/syntax]Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Re: i get an error
Posted: Sun Sep 23, 2007 8:09 pm
by Zoxive
yacahuma wrote:I get an error on this. document.getElementById(id) has no properties
Code: Select all
<p><label id="orderdate" for="orderdate">Date:</label><script>DateInput('orderdate', true, 'DD-MON-YYYY')</script></p>
... You didn't give the Label an Id.