Javascript equivalent of CSS border?

E

effendi

Hi

Can any tell me what is the javascript equivalent of CSS border? I
would like to change the border of my cell when it is set on focus. I
have tried onFocus="style.border='3px'" but it is not working.

Thanks
 
L

Lasse Reichstein Nielsen

Can any tell me what is the javascript equivalent of CSS border?

Borders are inherently presentation, and as such CSS (or old HTML).
However, you can use Javascript to access the CSS style of an object
through the W3C Style DOM.
I would like to change the border of my cell when it is set on
focus. I have tried onFocus="style.border='3px'" but it is not
working.

Close. Try:
onfocus="this.style.border='3px solid red';"
or just
onfocus="this.style.borderWidth='3px';"

Good luck
/L
 
N

Nathan

why not use the CSS pesudo-element :hover ?

:hover works for all elements. It's only IE that refuses to handle it
on anything other than <a>
 
G

Grant Wagner

Hi

Can any tell me what is the javascript equivalent of CSS border? I
would like to change the border of my cell when it is set on focus. I
have tried onFocus="style.border='3px'" but it is not working.

I doubt you want the border turned on when the cell has the focus set on
it (that would require the user actually click on the cell). Also,
Gecko-based browsers and Opera have no concept of "focus" on a table
cell:

<table><tr><td onfocus="alert('hi');">TEST</td></tr></table> does
nothing in a Gecko-based browser or Opera, no matter how much you click
and move the mouse around.

So, you probably want to do this onmouseover and onmouseout (to clear
the border when they leave the cell):

<script type="text/javascript">
function mOver(cell)
{
if (cell.style && 'undefined' != typeof cell.style.border)
{
cell.style.border = '3px solid Black';
}
}
function mOut(cell)
{
if (cell.style && 'undefined' != typeof cell.style.border)
{
cell.style.border = 'none';
}
}
</script>
<table>
<tr>
<td onmouseover="mOver(this);" onmouseout="mOut(this);">TEST</td>
<td onmouseover="mOver(this);" onmouseout="mOut(this);">TEST</td>
<td onmouseover="mOver(this);" onmouseout="mOut(this);">TEST</td>
</tr>
</table>

Note this creates a rather undesirable rearranging of the table as you
move the mouse around. Personally I'd probably do something like:

<style type="text/css">
td { border: 3px solid White; }
td.mOver { border: 3px solid Black; }
</style>
<script type="text/javascript">
function mOver(cell)
{
if ('undefined' != typeof cell.className)
{
cell.className = 'mOver';
}
}
function mOut(cell)
{
if ('undefined' != typeof cell.className)
{
cell.className = '';
}
}
</script>
<table>
<tr>
<td onmouseover="mOver(this);" onmouseout="mOut(this);">TEST</td>
<td onmouseover="mOver(this);" onmouseout="mOut(this);">TEST</td>
<td onmouseover="mOver(this);" onmouseout="mOut(this);">TEST</td>
</tr>
</table>

Of course, in browsers that support it (Gecko-based browsers, Opera),
all this can be replaced by:

<style type="text/css">
td { border: 3px solid White; }
td:hover { border: 3px solid Black; }
</style>
<table>
<tr>
<td>TEST</td><td>TEST</td><td>TEST</td>
</tr>
</table>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top