Error in document.getElementById(id).style.background-position = "-200px -500px"?

G

Ghyslaine Crespe

Hello,

In my script, the line

document.getElementById(id).style.background-position = "-200px -500px";

fails !

So, how can I change the background-position value ?

Thanks, Denis

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">

<style>
div.hr {
border: solid #0000FF;
display: inline;
height: 600px;
width: 300px;
background-image: url(plan_station.gif);
background-repeat: no-repeat;
background-position: -100px -400px;
margin: 0em 0 0em 0;
}

</style>

<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">

function goRight(id)
{
document.getElementById(id).style.height="100px";
document.getElementById(id).style.background-position = "-200px -500px";
}



</SCRIPT>
</HEAD>
<BODY>
<a href="#" onclick="goRight('myDiv')">right</a>
<DIV id="myDiv" class = "hr" ></DIV>
</BODY>
</HTML>
 
A

Andrew Poulos

Ghyslaine said:
Hello,

In my script, the line

document.getElementById(id).style.background-position = "-200px -500px";

[snip]


With hyphenated style properties you are usually meant to refer to them
in code by dropping the hyphen and capitalising the next word (the
intercapitalisation system). In your case, "background-position" becomes
"backgroundPosition"

Andrew Poulos
 
M

Michael Winter

On Sun, 26 Dec 2004 00:40:08 +0100, Ghyslaine Crespe

[snip]
So, how can I change the background-position value ?

Whilst background-position is the correct property name in CSS, it's an
illegal identifier in ECMAScript. You're looking for backgroundPosition.

[snip]
background-image: url(plan_station.gif);
background-repeat: no-repeat;
background-position: -100px -400px;

Be careful when specifying a background image without a background colour.
Make sure that the foreground content is still easily viewable if images
are disabled.
margin: 0em 0 0em 0;

That should be reduced to

margin: 0;

[snip]
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">

The language attribute is deprecated and besides, specifying the
(required) type attribute makes it redundant.
function goRight(id)
{
document.getElementById(id).style.height="100px";
document.getElementById(id).style.background-position
= "-200px -500px";

It would be more efficient to save a reference to the element, rather than
look it up twice:

var style = document.getElementById(id).style;
style.height = '100px';
style.backgroundPosition = '-200px -500px';

Preferably, it would include feature detection. Something like

var obj, style;
if(document.getElementById && (obj = document.getElementById(id))
&& (style = obj.style))
{
style.height = '100px';
style.backgroundPosition = '-200px -500px';
}

[snip]
<a href="#" onclick="goRight('myDiv')">right</a>

If you don't cancel the click event here, the vertical position of the
document may change:

onclick="goRight('myDiv');return false;"

Hope that helps,
Mike


Merry Christmas
 
G

Ghyslaine Crespe

Yes, it helps me a lot. Thanks for all these clues!

Denis

Michael Winter said:
On Sun, 26 Dec 2004 00:40:08 +0100, Ghyslaine Crespe

[snip]
So, how can I change the background-position value ?

Whilst background-position is the correct property name in CSS, it's an
illegal identifier in ECMAScript. You're looking for backgroundPosition.

[snip]
background-image: url(plan_station.gif);
background-repeat: no-repeat;
background-position: -100px -400px;

Be careful when specifying a background image without a background colour.
Make sure that the foreground content is still easily viewable if images
are disabled.
margin: 0em 0 0em 0;

That should be reduced to

margin: 0;

[snip]
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">

The language attribute is deprecated and besides, specifying the
(required) type attribute makes it redundant.
function goRight(id)
{
document.getElementById(id).style.height="100px";
document.getElementById(id).style.background-position
= "-200px -500px";

It would be more efficient to save a reference to the element, rather than
look it up twice:

var style = document.getElementById(id).style;
style.height = '100px';
style.backgroundPosition = '-200px -500px';

Preferably, it would include feature detection. Something like

var obj, style;
if(document.getElementById && (obj = document.getElementById(id))
&& (style = obj.style))
{
style.height = '100px';
style.backgroundPosition = '-200px -500px';
}

[snip]
<a href="#" onclick="goRight('myDiv')">right</a>

If you don't cancel the click event here, the vertical position of the
document may change:

onclick="goRight('myDiv');return false;"

Hope that helps,
Mike


Merry Christmas
 
E

Evertjan.

Ghyslaine Crespe wrote on 26 dec 2004 in comp.lang.javascript:
document.getElementById(id).style.background-position

document.getElementById(id).style.backgroundPosition
 

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

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,689
Members
48,796
Latest member
Greg L.

Latest Threads

Top