Very Strange undefined ID issue

Hello,

today i am facing a strange problem that i never face before.

form.php

<td><div id="test"><? echo $project;?></div></td>

ajax.js


var test=document.getElementById("test").value;
alert(test);

out put is ‘undefined’

form.php

<td><input id="test" name="" type="text" value="<? echo $project;?>" /></td>

ajax.js


var test=document.getElementById("test").value;
alert(test);

out put is ‘G4’ which works!!

So why it is not working when i put the php value inside a div? i also tried a label. that also not working. coz i do not want to use a text box here.

:rolleyes:

Hi there,

The first example evaluates to this:

<td><div id="test">G4</div></td>

You can get the text G4 with innerHTML

var test=document.getElementById("test").innerHTML;
alert(test);

This fails:

var test=document.getElementById("test").value;
alert(test);

because the test div has no value attribute.

Whereas the second snippet evaluates to:

<td><input id="test" name="" type="text" value="G4" /></td>

Therefore this works:

var test=document.getElementById("test").value;
alert(test);

as the test div has a value attribute.

Well understood sir!!!