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

Proper use of HTML and CSS

MB
Hello everyone!

These lines were inspired by the "DIV without line breaks" posted by
Johnny Two Dogs on June 22.

I know it is an old issue, but I want to share a few remarks that could
be of interest for many members of this group.

A brief summary is given below. However, you may want to take a look
back at the original posting before continuing.

The problem was the replacement of some-text with some-other-text. Each
piece of text was wrapped in its own div, each pair of divs being
hosted in a column of a three column table. The replacement should
occur at a mouse click on an additional anchor. The replacement itself
would be carried out by hiding or revealing the appropriate div via a
javascript function. A simplified representation of the problem is
given below:

<td><div>some-text</div><div>some-other-text</div></td>
<br>
<a onclick="do-the-swapping">click-here-to-do-the-swap</a>

I am not going to explain the difference between visibility and display
that prompted Johnny's question - there were 12 posts about this at the
time (which makes mine the 13th Yuck!).

Instead I want to question if that complicated structure was really
needed. Let me explain this a little.

If you think of the innerText property that just about any html element
holding text exposes, then that heavy html structure can be reduced to:

<td>some-text<td>

The swap routine's duty would thus be to check if td.innerText equals
or not some-text and replace it with some-other-text accordingly (see
code snippet below).

As far as I know, both Internet Explorer and Netscape do support the
innerText property. For the browsers that do not support it, but at
least support the DOM (why bother if they don't?), one more step is
required.
From DOM perspective (hierarchy), the element holding data (td in this

case) is a node. To capture the text content of the node, one could use
this scheme:

1. get a reference to the node
some-node = document.getElementById('id-of-td-element').firstChild;

2. compare text content of the node with some-text and swap it
accordingly
if some-node.data = some-text then some-node.data = some-other-text
else some-node.data = some-text
The use of the anchors (links) to trigger the swapping action, is a
matter of taste, perhaps a project requirement, and I only want to show
that, if desired, one could get rid of them too, making the html even
lighter.

First, we must provide the user with a clue that something is clickable
in our table. Nothing easier. We could use the onMouseOver event with
some code like this:

<td id="td1"
onMouseOver="javascript:this.style.cursor='hand';" >some-text</td>

But why clutter our html with such mile-long javascript code, when we
could use a very simple css rule instead:

..clickMe {cursor: hand;}

<td id="td1" class="clickMe">some-text</td>

Finally, the triggering of the swapping process itself can be done
using the onClick event:

<td id="td1" onclick="javascript:swap-them();">some-text</td>
These said, here's what I think to be a light solution to Johnny's
problem (doctype and other stuff omitted):

<html>
<head>
<style>
td {cursor: hand;} /* assume only one table in the html file */
</style>
<script>
function swapEm(elem, newValue, oldValue)
{elem.innerText = ( oldValue == elem.innerText )? newValue : oldValue;}
</script>
</head>
<body>
<table width="100%" border="1" cellspacing="0" cellpadding="4">
<tr>
<td id="cell1" onclick="javascript:swapEm(this,'Changed 1','Original
1');">Original 1</td>
<td id="cell2" onclick="javascript:swapEm(this,'Changed 2','Original
2');">Original 2</td>
<td id="cell3" onclick="javascript:swapEm(this,'Changed 3','Original
3');">Original 3</td>
</tr>
</table>
</body>
</html>
In the event your boss insists to keep that complicated structure, css
might also help make things easier. Using the className property, one
could create css rules similar with these:

..concealed {display: none;}
..revealed {display: block; visibility: visible;}

<td><div class="revealed">some-text</div><div
class="concealed">some-other-text</div>

and then use the className in the comparison routine. Of course, here
we deal with multiple objects and thus the comparison routine should
look something similar to that posted by RobG, using td.className
instead of div.style.display (this is really a matter of taste,
whichever better suits you).

These said, I hope I gave you something interesting to think about and
wish everybody Happy HTML Programming!

Aug 5 '05 #1
4 4100
MB wrote:
First, we must provide the user with a clue that something is clickable
in our table. Nothing easier. We could use the onMouseOver event with
some code like this:

<td id="td1"
onMouseOver="javascript:this.style.cursor='hand';" >some-text</td>

But why clutter our html with such mile-long javascript code, when we
could use a very simple css rule instead:

.clickMe {cursor: hand;}

<td id="td1" class="clickMe">some-text</td>


A mouseover effect is not a useful clue that a page element is
clickable. To be useful, a clue has to be apparent when the user's
*eyes* are on the element, not when the *cursor* is on it. If you
haven't already given him reason to think the element is clickable, why
would you assume a user would *ever* run the cursor over it?
Aug 8 '05 #2
MB
Thanks for noticing this 'small' omission! The style sheet should've
also included the td {text-decoration: underline ;} rule, which is
obtainable WITHOUT extra elements (i.e. the anchor element). This was
actually the idea of my posting: light html and simple css rules.

Harlan Messinger wrote:
A mouseover effect is not a useful clue that a page element is
clickable. To be useful, a clue has to be apparent when the user's
*eyes* are on the element, not when the *cursor* is on it. If you
haven't already given him reason to think the element is clickable, why
would you assume a user would *ever* run the cursor over it?


Aug 8 '05 #3
MB
To better undestand what I mean by simplier html code, consider the
original posting:

<ol>
<li><a href="#" onClick="java-script-code;">click column 1 to change
value</a></li>
<li><a href="#" onClick="java-script-code;">click column 2 to change
value</a></li>
<li><a href="#" onClick="java-script-code;">click column 3 to change
value</a></li>
</ol>

In my version, one could say <p>Click values in the table to see them
changing</p> which is noticeable shorter. (This could be a clue for the
case they do not want the values in the table to be underlined)

Aug 8 '05 #4
MB wrote:
[...]
If you think of the innerText property that just about any html element
holding text exposes, then that heavy html structure can be reduced to: [...] As far as I know, both Internet Explorer and Netscape do support the
innerText property. For the browsers that do not support it, but at
Microsoft's proprietary innerText is not widely supported beyond IE -
it is not supported in Firefox or Safari.
least support the DOM (why bother if they don't?), one more step is
required.
Given that any browser since IE 5 that supports innerText will almost
certainly support DOM, it may be the better choice particularly if the
structure is simple (as per your example). The non-standard but
widely supported innerHTML with a regular expression can also be used
to good effect to simulate innerText.

DOM 3 nodes have a textContent attribute, but support is not yet
widespread.

[...]

<td id="td1"
onMouseOver="javascript:this.style.cursor='hand';" >some-text</td>
[...]
Finally, the triggering of the swapping process itself can be done
using the onClick event:

<td id="td1" onclick="javascript:swap-them();">some-text</td>


The use of the javascript pseudo protocol here is unnecessary (unless
some other scripting language has been used previously). Removing it
nearly halves the code (if you're concerned about 'lightness' :-) ).

[...]

--
Rob
Aug 9 '05 #5

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

Similar topics

1
by: Vlajko Knezic | last post by:
Not so sure what is going on here but is something to do with the way UTF8 is handled in Perl and/or LibXML The sctript below: - accepts a value from a form text field; - ...
0
by: Neil Zanella | last post by:
Hello, I would like to know whether it is possible to get an XSLT processor to output tags with the proper indentation. For instance, given the following standard example I get the output given...
8
by: CMAR | last post by:
I create my website using Front Page 2000. I notice that none of my pages have a DocType statement at the top. I have read that if you want IE6 to use "Standards mode" rather than the "Quirks...
2
by: Ray | last post by:
I want to put a navigation bar on a page like: <img> Home >> My Family >> My Daughters >> Melissa >> Three-year-old First I used <ul class="breadcrumb"> <li><img src="icon.gif" alt="icon"><a...
59
by: Haines Brown | last post by:
I've not had a clear definition in my mind of "citation," and so have avoided it. For example, if I suggest that the reputation of the New York Times has suffered, is that a citation? I suppose...
9
by: Richard Silverstein | last post by:
I was hoping someone more expert in css could help me w. a problem I'm having w. a stylesheet I've created for a Pbase photo site. My site is at http://www.pbase.com/richards1052. Currently, it...
14
by: Viken Karaguesian | last post by:
Hello all, It stinks being a newbie! One thing that I'm not sure about with CSS is: where is the "proper" place to put font attibutes? This has me a little confused. Take the below style sheet...
0
drhowarddrfine
by: drhowarddrfine | last post by:
The Doctype or DTD Many coders get confused by all the talk of doctypes and how they affect browsers and the display of their web pages. This article will get right to the point about doctypes...
1
by: Hendrik Maryns | last post by:
Hi, I’ve read Jukka’s page about iframes and generally noted the advice not to use it, so I am looking for a proper way to handle this: http://www.weltladen-tuebingen.de/index_kontakt.html...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.