/    Sign up×
Community /Pin to ProfileBookmark

Using the DOM to show or hide a <br /> tag.

Hi All,

I’ve got a bit of code in a form which shows, or hides, a label and its corresponding input / selectbox, depending on whether or not it has been selected.

Each element in the form is seperated by a <br /> tag, (to which I’ve assigned a style of “clear:both;” ). I use this to line up each elements using CSS, instead of tables.

…So far, so good….

However, the problem I was recently having was that when an element was hidden, the <br /> which followed was still displayed – resulting in a lot of “blank” space – not very pretty.

As a result, I’m trying hide the appropriate <br />’s, and only show them when necessary.

The problem is, although I have managed to get the appropriate <br />’s to hide, I can’t seem to get them to “reappear” or to reinsert the “clear:both” style which I need to ensure the rest of the form lines up correctly.

Here’s the code I’m using so far to show / hide the form elements and the corresponding <br />

[code]function showstuff(level){
document.getElementById(‘level_’+level).className=”levelshow”;
form.getElementsByTagName(“label”)[level].className=”labelshow”;
var aBr=form.getElementsByTagName(“br”);
for(var i=1; i<aBr.length; i++)
if (i<4)
{
aBr[i].className=”brshow”;
}
}

function hidestuff(level){
var aLabel=form.getElementsByTagName(“label”);
for(var i=level; i<aLabel.length; i++)
if (i<4)
{
document.getElementById(‘level_’+i).className=”levelhide”;
aLabel[i].className=”labelhide”;
}
var aBr=form.getElementsByTagName(“br”);
for(var i=1; i<aBr.length; i++)
if (i<4)
{
aBr[i].className=”brhide”;
}
}[/code]

What happens is that the <br />’s are removed, but I don’t seem to be able to get them reinstated when required. This means that the following form elements float incorrectly.

Does anyone have any ideas where I may be going wrong?

Oh yeah, and here are the styles: ?

.brshow {display:block; clear:both;}
.brhide {display:none;}

Cheers
Kessa

to post a comment
JavaScript

15 Comments(s)

Copy linkTweet thisAlerts:
@FangApr 01.2006 — Maybe the labels should be [I]display:block[/I] and if necessary a [I]margin[/I].

That should avoid using the <br>
Copy linkTweet thisAlerts:
@kessaauthorApr 01.2006 — Hi Fang,

Cheers, unfortunately I've already tried that (and all the usual CSS tricks which I would normally expect to work) and I've even tried adding the style manually to the last <br /> - still no joy.

It seems to be that once the br is set to "display:none;" it doesn't want to appear again and so I wondered if it was something with the javascript?

Any other ideas?

Cheers

Kessa
Copy linkTweet thisAlerts:
@kessaauthorApr 01.2006 — Hi Fang,

Actually, you'll be pleased to know that you were right (in a way) ?

Although I had changed the styles to "display:block;" (and it initially hadn't had any effect), your suggestion prompted me to take another look at the styles and I noticed that one of the other styles used by the scrpt still had an inline style which I had overlooked.

Here are the amended styles:

.levelshow {display:block;}

.levelhide {display:none;}

.labelshow {display:block;}

.labelhide {display:none;}

.brshow {display:block; clear:both;}

.brhide {display:none;}

So it's now working a treat!

Cheers ?

Kessa
Copy linkTweet thisAlerts:
@kessaauthorApr 01.2006 — Actually, one final thought.....

At the moment I get a temporary "flash" of the 3 submenus as the page loads.

Is there a way to hide the submenu's (and corresponding labels / <br />):

a) if javascript is disabled.

b) from the instant the script starts to load (rather than when the page has loaded)

I've seen some solutions which use an inline class which is set to "display:none;" by default and then updated using the DOM... and I think that this may be what I need to do... but I'm not sure how I would need to intergate this into the script above.

Cheers

Kessa
Copy linkTweet thisAlerts:
@FangApr 01.2006 — a) The document should be fully functional without JavaScript. Is it?

b) http://dean.edwards.name/weblog/2005/09/busted2/
Copy linkTweet thisAlerts:
@kessaauthorApr 01.2006 — Hi Fang,

The document should be fully functional without JavaScript. Is it?
[/quote]


Yes and no... but that's deliberate.

As I'm using the select boxes to provide a hierarchy of destinations, I only want to provide the [I]full [/I] functionality [I]if [/I] javascript is enabled.

Otherwise (ironically) the feature would actually be less usable.

This is because:

a) there could be hundreds (and eventually, perhaps even thousands) of options to choose from (not very use friendly ? )

b) the names / relationships may only make sense when the script is [I]fully [/I] functional.

For example - there is a "Boston" in the UK, and a "Boston" in the USA - however, this would only make sense to people where the script is fully functional.

That said, the top "country" level is always available and so the form still remains usable.

What I am trying to do is to help the script degrade even more gracefully by only displaying the other options, of javascript is available.

Is there a way for me to give the elements which I want to hide (whilst the page is loading / if javascript is not enabled) a style (lets say class="onloadhide") and then replace that style once required by the script above?

Cheers again!

Kessa
Copy linkTweet thisAlerts:
@KorApr 01.2006 — do it with javascript. javascript disabled users are less and less. And, after all, this is their option. No javascript, no page.
Copy linkTweet thisAlerts:
@kessaauthorApr 01.2006 — Hi Kor,

Nice idea, but I'm a firm believer in trying to make as much of a site as accessible and usable as possible, and although I appreciate it's often tempting, I don't think it's fair to penalise people who don't have access to javascript (remember - that doesn't necessarily mean people have turned if off!)

For example, screenreaders and search engines are just 2 regular visitors to the site that we will want to cater for.

Penalise a search engine and that's bad news :rolleyes: , penalise a screenreader and that's just a lack of manners ?

Kessa
Copy linkTweet thisAlerts:
@felgallApr 01.2006 — do it with javascript. javascript disabled users are less and less. And, after all, this is their option. No javascript, no page.[/QUOTE]

Being able to use Javascript is NOT an option for blind people because none of the browsers that they are able to use support Javascript. There have also been instances of blind people taking web site owners to court because their site is unusable.
Copy linkTweet thisAlerts:
@KorApr 02.2006 — 
none of the browsers that they are able to use support Javascript.
[/quote]

I am not so sure. I think there are some, as I've heard... Anyway I should have give a nuance. Use javascript as long the code would not affect seriouselly the navigation nor the important information on the page.
Copy linkTweet thisAlerts:
@kessaauthorApr 02.2006 — javascript issues aside..... ? ..... I've managed to find a (surprisingly simple) way to hide the appropriate bits of code until required AND if javascript available..

... Just added a [B]class[/B] to each of the elements (in the XHTML) which I wanted to hide.....

....then using the DOM, assign a new class to show the element when required (and if Javascript is available ? )

.... simple, effective, and user friendly... I love the DOM ?

Kessa
Copy linkTweet thisAlerts:
@phpnoviceApr 02.2006 — There have also been instances of blind people taking web site owners to court because their site is unusable.[/QUOTE]
There are no laws governing web site accessibility except as concerns the federal government's own web sites. Hence, cite the state, locality, case number, and verdict when you make such claims. Otherwise, you're just blowing smoke (same thing as lying).
Copy linkTweet thisAlerts:
@FangApr 02.2006 — Just added a class to each of the elements (in the XHTML) which I wanted to hide[/QUOTE]Without JavaScript this will not work, not user friendly.
Copy linkTweet thisAlerts:
@kessaauthorApr 02.2006 — Hi Fang,

Without JavaScript this will not work, not user friendly[/quote]

Normally, I would have to agree with you 100% on this.... especially as I'm a huge fan of making things as accessible as possible to all users.

However, in this instance I [I]deliberatly don't [/I] want to give full access to the feature if javascript is unavailable for the reasons stated above.

In this instance, if javascript is turned off, the end user only sees the "Country" option, and[B] is not [/B] confronted with 3 other select boxes with hundreds of options to have to sift through manually.

This means that they can still use the search feature - but just without the "bells and whistles" that the DOM / Javascript would provide. ?

I guess it would make more sense to see what I mean when the script is live - I'll add a link to it hear once it's all completed.

Cheers

Kessa
Copy linkTweet thisAlerts:
@kessaauthorApr 02.2006 — Update:

Actually, the only browser which I'm still having trouble with is Netscape 7.

I think this is happening because of the bug with Netscape which doesn't allow certain styling to be applied direct to a form <label>

Instead, you have to place an empty <span> within the <label> and apply the styles to that span.

[B]For Example:[/B]

[I]<label for="level_1" >[COLOR=Orange]<span>[/COLOR]Region[COLOR=Orange]</span>[/COLOR]</label>[/I]

[B]Style: [/B]

[COLOR=Gray]form label span {/*apply styles*/}[/COLOR]

The problem with the script above is that it changes the styles for the [I]label [/I] rather than the [I]span[/I].

Is there a way to assign a style to a span within a label?

Here's the existing code:
[CODE]form.getElementsByTagName("label")[level].className="labelshow";[/CODE]

I've tried:
[CODE]form.getElementsByTagName("span")[level].className="labelshow";[/CODE]

and I've tried...
[CODE]var aSpan=form.getElementsByTagName("span");
for(var i=1; i<aSpan.length; i++)
if (i<4)
{
aSpan[i].className="spanshow";
}[/CODE]


... but neither worked.

Anyone have any ideas? It works a treat in all of the other browsers which I've checked (and in my mobile) but it would be a shame if I couldn't get it to work in Netscape too.

Kessa

PS - would I be correct in thinking that Netscape 7 doesn't support [B]setAttribute[/B] and [B]removeAttribute[/B]?
×

Success!

Help @kessa 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 5.19,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...