473,386 Members | 1,819 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,386 software developers and data experts.

first element before my div

sc
I've got such structure of the part of the site:

<p>content content</p>
<div id=myDiv1>content content</div>

<p>content content</p>
<div id=myDiv2>content content</div>

No I want to hide let's say 'myDiv2'. So I write in JS:

document.getElementById('myDiv2').style.display = 'none'

And now, how to hide also the paragraph which is before the 'myDiv2' div ?
How to check if the first element before 'myDiv2' is a paraghraph ?

Regards,
sc
Apr 6 '06 #1
6 2799
sc wrote on 06 apr 2006 in comp.lang.javascript:
I've got such structure of the part of the site:

<p>content content</p>
<div id=myDiv1>content content</div>

<p>content content</p>
<div id=myDiv2>content content</div>

No I want to hide let's say 'myDiv2'. So I write in JS:

document.getElementById('myDiv2').style.display = 'none'

And now, how to hide also the paragraph which is before the 'myDiv2'
div ? How to check if the first element before 'myDiv2' is a
paraghraph ?


This is very browser dependent. I think that FF reeds the return as an
extra element, where IE does not?

The safe way is to enclose the two in an extra div.

Try:

<div>
<p>content 1 content</p>
<div id=myDiv2>content 2 content</div>
</div>

<script type='text/javascript'>
document.getElementById('myDiv2').parentNode.style .display = 'none'
</script>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Apr 6 '06 #2
sc
"Evertjan." wrote:
Try:

<div>
<p>content 1 content</p>
<div id=myDiv2>content 2 content</div>
</div>

<script type='text/javascript'>
document.getElementById('myDiv2').parentNode.style .display = 'none'
</script>


The trouble is that I'm not allowed to change the structure of that HTML...

Regards,
sc
Apr 6 '06 #3

sc wrote:
I've got such structure of the part of the site:

<p>content content</p>
<div id=myDiv1>content content</div>

<p>content content</p>
<div id=myDiv2>content content</div>

No I want to hide let's say 'myDiv2'. So I write in JS:

document.getElementById('myDiv2').style.display = 'none'

And now, how to hide also the paragraph which is before the 'myDiv2' div ?
How to check if the first element before 'myDiv2' is a paraghraph ?

Regards,
sc


Tried "previousSibling" ?

var somediv = document.getElementById("myDiv2");
var sibbling = somediv.previousSibling;

Note that it might select a "textNode" if you have whitespaces at odd
places.

Apr 6 '06 #4
sc wrote on 06 apr 2006 in comp.lang.javascript:
"Evertjan." wrote:
Try:

<div>
<p>content 1 content</p>
<div id=myDiv2>content 2 content</div>
</div>

<script type='text/javascript'>
document.getElementById('myDiv2').parentNode.style .display = 'none'
</script>
The trouble is that I'm not allowed to change the structure of that
HTML...


I would not accept an assignment with susch restrictions.

given your question: How to check if the first element before 'myDiv2' is a
paraghraph ?


you could check that:

<div>
<p>content 1 content</p>
<div id=myDiv2>content 2 content</div>
</div>

<script type='text/javascript'>
alert(document.getElementById('myDiv2').previousSi bling.tagName=='P')
</script>

giving, as expected, true in IE and false in FF

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Apr 6 '06 #5
sc wrote:
"Evertjan." wrote:
Try:

<div>
<p>content 1 content</p>
<div id=myDiv2>content 2 content</div>
</div>

<script type='text/javascript'>
document.getElementById('myDiv2').parentNode.sty le.display = 'none'
</script>

The trouble is that I'm not allowed to change the structure of that HTML...


You can work with that, but it's a bit ugly. The following script returns
the previous sibling with the given node name or null if it can't be found:

<script type="text/javascript">

function getPreviousByNodeName(el, nodeName)
{
if (el.previousSibling){
while(el.previousSibling && (el = el.previousSibling)){
if (nodeName.toLowerCase() == el.nodeName.toLowerCase()){
return el;
}
}
return null;
}
}

</script>
<p>the p</p>
<div
onclick="alert(getPreviousByNodeName(this, 'p').innerHTML);"click to get P</div>


Of course the value returned by getPreviousByNodeName() should be tested
before trying to use it, e.g.

var theP = getPreviousByNodeName(el, 'p');
if (theP){
// do stuff
}

--
Rob
Apr 6 '06 #6
sc
Big thanks for help to everyone !

sc
Apr 6 '06 #7

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

Similar topics

2
by: Neal D. Becker | last post by:
What is an efficient way to find the first element in a sequence meeting some condition? For example, the first number > x in a list of numbers.
12
by: Alan J. Flavell | last post by:
OK, today's email brought a comment from a reader, pointing out something that I'd long since noticed myself but hadn't done anything about it. On my pages, I've got a first-letter style on...
1
by: Stanimir Stamenkov | last post by:
I've wondered if it is right thing to do: element.parentNode.insertBefore(element, beforeElement); where 'beforeElement' is one of the 'element.parentNode.childNodes'? First, I've used: ...
1
by: huzheng001 | last post by:
I have develop a on-line dictionary website, http://www.stardict.org I meet a problem: Here is two lines of js codes: document.getElementById("wordlist").innerHTML = "";...
13
by: howa | last post by:
e.g. <div class="main"> <p>ssssss</p> <p>ssssss</p> <p>ssssss</p> <p>ssssss</p>
3
by: jm.suresh | last post by:
Hi, I have a list and I want to find the first element that meets a condition. I do not want to use 'filter', because I want to come out of the iteration as soon as the first element is found. I...
10
by: Chris Forone | last post by:
Hello Group, there is some memberfunc for std::valarray to return a pointer to the first element in the array. How do i use this? Thanx a lot. HAND Chris
12
by: Phillip B Oldham | last post by:
I'm keen on learning python, with a heavy lean on doing things the "pythonic" way, so threw the following script together in a few hours as a first-attempt in programming python. I'd like the...
4
by: nembo kid | last post by:
I have the following bidimensional array int a ; Why the first address of this array is only: & (mat) and not also:
4
by: mab464 | last post by:
I have this code on my WAMP server running on my XP machine if ( isset( $_POST ) ) { for($i=0; $i<count($_POST);$i++) { if ($ans != NULL ) $ans .= ", " . $_POST ; // Not the first...
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.