Click to See Complete Forum and Search --> : need help with document.getElementById


zwomble
06-13-2003, 06:45 AM
Hi,
litte problem with my script:

i use a counter called "zaehler" to identify several div tags

now i want to get the properties of one div tag by using:

data = document.getElementById(zaehler).style.left;

this doesn`t work but if i write

data = document.getElementById('107').style.left;

the result is correct.

zaehler was of course initialized and has a value

so what is wrong with my code?
Thanxx for help

Zwomble

Charles
06-13-2003, 06:51 AM
Try data = document.getElementById(zaehler.toString()).style.left;.

zwomble
06-13-2003, 06:58 AM
sorry but doesn`t work.

am i allowed to use numbers for id`s???
perhaps that´sthe mistake???
Cu Zwomble

Charles
06-13-2003, 07:01 AM
Then post your URL.

Charles
06-13-2003, 07:03 AM
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
http://www.w3.org/TR/html4/types.html#type-id

zwomble
06-13-2003, 07:05 AM
the page isn`nt onlione yet but here are some parts of the source:
the js:

function getBoxes(){
zaehler = document.getElementById('new').value;
if(!zaehler) n=1;
alert('z:'+zaehler);
document.getElementById('new').value = zaehler++;
//document.getElementById('counterx'+ob).value = e.pageX;
//document.getElementById('countery'+ob).value = e.pageY;
document.getElementById('speichern').style.visibility = 'hidden';
document.getElementById('senden').style.visibility = 'visible';
document.getElementById('update').style.visibility = 'visible';
alert('n:'+zaehler);
while(zaehler >38){
alert('davor'+zaehler);
// data = document.getElementById(zaehler.toString()).style.left;
data = document.getElementById(zaehler.toString()).style.left;
zaehler--;
alert('2');
}
alert(data);
}


for example one div:
<div name="37" id=37 style="font-size:;background:#ffffff;position:absolute;width:154;height:55;left:28;top:542;border:1px #cc0000 solid;"><span id="3737" name="3737">Vergrößerung: 20 x<br />
Genauigkeit pro km Doppelniv.: ± 2,5 mm<br />
kürzeste Zielweite: 0,8 m</sapn></div>
<input type="hidden" name="counterx[37]" id="counterx37" value="28">
<input type="hidden" name="countery[37]" id="countery37" value="542">
<input type="hidden" name="counterh[37]" id="counterh37" value="55">
<input type="hidden" name="counterw[37]" id="counterw37" value="154">
<input type="hidden" name="alttext[37]" value="Vergrößerung: 20 x
Genauigkeit pro km Doppelniv.: ± 2,5 mm
kürzeste Zielweite: 0,8 m">
<input type="hidden" name="bg[37]" value="#ffffff">


sorry for this mess...
Zwomble

Khalid Ali
06-13-2003, 07:52 AM
In the code you posted I don't see a div or id attribute with value "new"????

and the convention of id attributes you are using will make it rather hard to implement a speedy solution to refer to the divs...
one last thing I don't see anyw hwere in the code posted where you are triggering this method call

getBoxes()

?????

allotsa ??? marks in your code...:D

zwomble
06-13-2003, 12:51 PM
I just posted the interesting part because the hole code would be too much...

everything works find until the line:
function getBoxes(){
zaehler = document.getElementById('new').value;
if(!zaehler) n=1;
alert('z:'+zaehler);
document.getElementById('new').value = zaehler++;
document.getElementById('speichern').style.visibility = 'hidden';
document.getElementById('senden').style.visibility = 'visible';
document.getElementById('update').style.visibility = 'visible';
alert('n:'+zaehler);
while(zaehler >38){
alert('davor'+zaehler);

data = document.getElementById(zaehler).style.left;

i found alredy out that only digits for an id are not allowed, so i trie to name the tag by letter + number

the field "new" has a value an d the function is called when you press a submit button thats why i didn´t put it in my tread.

there is somthing wrong with
>>data = document.getElementById(zaehler).style.left;

and i can`t figure out what....

Zwomble

Khalid Ali
06-13-2003, 02:24 PM
Well good luck with that...because your code does not show where the error is...ofcourse you claim its in there ...but thats your claim..isnt it..:D

SlankenOgen
06-14-2003, 10:28 AM
In this line

zaehler = document.getElementById('new').value;

zaehler is a string, yet you try to increment it as if it were a number with zaehler++;

?????????

~mgb

Charles
06-14-2003, 03:06 PM
Originally posted by SlankenOgen
In this line

zaehler = document.getElementById('new').value;

zaehler is a string, yet you try to increment it as if it were a number with zaehler++;

?????????

~mgb That shouldn't make a difference. If you try to increment a string primitive the primitive is first converted to a String wrapper object and then String.valueOf() is called.

<script type="text/javascript">
<!--
foo = '2';
alert (++foo)
// -->
</script>