473,387 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to display a child element while hiding a parent

Hi,

I am trying to display the child element in the DOM, while hiding the
parent using JS and CSS, however I cannot find a way to do this.

So for example:

<body>
<div id="Parent">
<div id="child_1"></div>
<p id="child_2"></p>
</div>
</body>

I am trying to hide "Parent", and "child_1" and show "child_2". I
cannot change the possition in the actual mark up so i need script to
do this.

and so far hiding Parent using:

$("Parent").style.display="none";

Hides all the child elements.

Do anyone know how I can do this? or an alternative method to achive
the same result?

Thanks in advance.
Jun 27 '08 #1
7 12327
On 4 Jun, 12:16, dwe...@gmail.com wrote:
Hi,

I am trying to display the child element in the DOM, while hiding the
parent using JS and CSS, however I cannot find a way to do this.

So for example:

<body>
<div id="Parent">
<div id="child_1"></div>
<p id="child_2"></p>
</div>
</body>

I am trying to hide "Parent", and "child_1" and show "child_2". I
cannot change the possition in the actual mark up so i need script to
do this.

and so far hiding Parent using:

$("Parent").style.display="none";

Hides all the child elements.

Do anyone know how I can do this? or an alternative method to achive
the same result?
Just hide all the child elements that you do not want?

Jun 27 '08 #2
On 4 Jun, 15:54, Captain Paralytic <paul_laut...@yahoo.comwrote:
On 4 Jun, 12:16, dwe...@gmail.com wrote:


Hi,
I am trying to display thechildelement in the DOM, while hiding the
parentusing JS and CSS, however I cannot find a way to do this.
So for example:
<body>
<div id="Parent">
* *<div id="child_1"></div>
* *<p id="child_2"></p>
</div>
</body>
I am trying to hide "Parent", and "child_1" and show "child_2". *I
cannot change the possition in the actual mark up so i need script to
do this.
and so far hidingParentusing:
$("Parent").style.display="none";
Hides all thechildelements.
Do anyone know how I can do this? *or an alternative method to achive
the same result?

Just hide all thechildelements that you do not want?- Hide quoted text -

- Show quoted text -
Its not quite that simple, as this is a small example the one i'm
using has hundreds, also the parent does formatting which i want to
remove, so hiding parent would take care of all of this in one hit.
Hididng and changing styles for all would mean lines and lines of
code. Thanks
Jun 27 '08 #3
Its not quite that simple, as this is a small example the one i'm
using has hundreds, also the parent does formatting which i want to
remove, so hiding parent would take care of all of this in one hit.
Hididng and changing styles for all would mean lines and lines of
code. Thanks
You could create a sibling to the parent element and place it directly
after that parent element.

Then move the child you want to save to the new parent

Then make the old parent invisible

Might need some refinement, but the principle is there
Jun 27 '08 #4
On 4 Jun., 13:16, dwe...@gmail.com wrote:
<body>
<div id="Parent">
<div id="child_1"></div>
<p id="child_2"></p>
</div>
</body>

I am trying to hide "Parent", and "child_1" and show "child_2". I
cannot change the possition in the actual mark up so i need script to
do this.
Not necessarily, and not here. If scripting CSS can do this, plain
CSS can do it as well.
and so far hiding Parent using:

$("Parent").style.display="none";

Hides all the child elements.
Works as designed. Setting the `display' property to `none' renders
the respective element as if it never existed, so not at all. If you
accomplished time-travel and rendered your parents non-existing before
the time you were conceived, you would not exist (at least that is the
known paradox).
Do anyone know how I can do this? or an alternative method to achive
the same result?
The equivalent of

$("Parent").style.visibility = "hidden";
$("child_2").style.visibility = "visible";

works in Firefox 2.0.0.14, IE 6.0.2900.2180.xpsp_sp2_gdr.070227-2254,
IE 7.0.5730.11, Opera 9.27.8841, and Safari 3.1.1 (525.17) on Windows
XP SP2. The CSS2 Specification, section 11.2, suggests that this is
compliant behavior:

http://www.w3.org/TR/REC-CSS2/visufx...def-visibility

You might have to do the equivalent of

$("Parent").style.overflow = "hidden";

to get rid of then-unnecessary scrollbars, too.
HTH

PointedEars
Jun 27 '08 #5
On 4 Jun, 23:27, Dan Rumney <danrum...@77617270mail.netwrote:
* Its not quite that simple, as this is a small example the one i'm
using has hundreds, also theparentdoes formatting which i want to
remove, sohidingparentwould take care of all of this in one hit.
Hididng and changing styles for all would mean lines and lines of
code. *Thanks

You could create a sibling to theparentelementand place it directly
after thatparentelement.

Then move thechildyou want to save to the newparent

Then make the oldparentinvisible

Might need some refinement, but the principle is there
Thank you all for your help.

Dan, I've played around with scripting the concept you've mentioned,
it's quite abit of manipulation but its the best method I've seen so
far and it does exactly what I need.

Thanks again.
Jun 27 '08 #6
SAM
dw****@gmail.com a écrit :
>
Dan, I've played around with scripting the concept you've mentioned,
it's quite abit of manipulation
not so much

function $(id) { return document.getElementById(id); }
function childSchow(parent, child) {
child = $(child).cloneNode(true);
child.className = '';
child.id += 'c';
var parent = $(parent);
parent.parentNode.insertBefore(child, parent);
child.memory = parent.parentNode.removeChild(parent);
}
function parentSchow(child) {
child = $(child+'c');
var parent = child.memory
child.memory = null;
child.parentNode.insertBefore(parent, child);
child.parentNode.removeChild(child);
}
not tested with IE
--
sm
Jun 27 '08 #7
xomby
1
The goal:
Need to hide a parent <div> (or any object, just using div as an example here), but display its children.

The example html code:
Expand|Select|Wrap|Line Numbers
  1. <div id="parent-div">
  2. <div id="child-div-01">Child Div 01</div>
  3. <div id="child-div-02">Child Div 02</div>
  4. </div>
  5.  
The css code that accomplishes this (using jQuery):
Expand|Select|Wrap|Line Numbers
  1. $('#parent-div').css('visibility','hidden'); //selects the parent-div and sets it to invisible
  2. $('#parent-div > div').css('visibility','visible'); //selects all children of parent-div that are also of type div and sets them to visible
NOTE:
If you're using this, and trying to hide a "styled" parent-div, you may notice that (as it should) the parent-div still takes up space on the screen...
In my case, I was hiding a div that was part of a vertical menu on a webpage, because I wanted to re-use those links in a menu that followed the user as she scrolled down the page, however I wanted to make sure that without javascript, the menu would still show up as a vertical menu. So I changed the firs tline to the following to "reclaim" the space taken up by the div.
Expand|Select|Wrap|Line Numbers
  1. $('#parent-div').css({'visibility':'hidden','width':'0px','height':'0px','margin':'0','padding':'0'});
This effectively hid the parent-div and neutralized its usage of space.

You could also do this with straight CSS (probably better to do by class for the children instead of by id:
Expand|Select|Wrap|Line Numbers
  1. #parent-div{ //by id
  2. visibility:hidden
  3. width:0px;
  4. height:0px;
  5. margin:0;
  6. padding:0;
  7. }
  8. #parent-div div.child-div{ //by class
  9. visibility:visible;
  10. }
I hope someone finds this to be useful!

~Xomby
Dec 15 '11 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rob T. | last post by:
Given the following in a stylesheet: #parent { width: 50px; height: 50px; } #child {
2
by: Chandru | last post by:
hi I want to know how to pass values from different forms. That is, i created one parent form. from there i opened child form modeless way. Once i close the child form i need some value from...
4
by: Sean Connery | last post by:
I know I can merge a child forms menu into the MDI parent's mainmenu, but I would also like to listen for common events fired from the mainmenu that might be of interest to any child forms. Is...
2
by: lotus | last post by:
HI All.. I'm realtively new to C#. I have MainForm which includes Parent usercontol, and this parent usercontrol also contains child usercontrol. MainForm --> Parent usercontrol --> child...
3
by: William Krick | last post by:
Given this XML <parent> <child name="billy"> <child name="sue"> </parent> I can use this to process each child... <xsl:for-each select="child">
4
by: patrizio.trinchini | last post by:
Hi all, I'm new to XSLT and maybe my problem have a very trivial answer, but I need an expert that point me in the right direction. What I would obtain is to remove all the elements that have a...
3
by: rlueneberg | last post by:
I want to change the color of an "a" child element inside a table cell via javascript. Is there any way to do that? Ps: there is no id assigned to child elements. I would like to discover the child...
1
by: kumuda v | last post by:
Hello all, I am new to web programming.I have an issue that has to done using javascript and php. How can I pass the data from child window to parent window. I have already written the...
5
by: BibhuAshish | last post by:
Hello, I have one child window. in child window i am selecting one radio button. i want to return that selected value to parent window. To open child window in parent window, i have written like...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.