views:

185

answers:

1

hello,

I've a table in which addition rows can be generatred as per the user need by clicking a javascript function.

Each row has a drop down list, and based on the values of this an AJAX script fetchs some values which has to be displayed in corresponding textfields of the same row..

here is the code for HTML..

<td><div align="center">

            <label>
            <select name="gcno1" id="gcno1"  onchange="fetch_gc(this)">
             <option value="0">NIL</option>
             <option value="2">1</option>
            <?php while($row=mysql_fetch_array($result))
                    {
            ?>
              <option value="<?php echo $row[0]; ?>"><?php echo $row[0]; ?></option>
              <?php }?>
            </select>
            </label>
          </div></td>
          <td><div align="center"><input name="date1" id="date1" type="text" size="10" />
          </div></td>

and here is the AJAX which I'm writing...

    xmlhttp = new XMLHttpRequest();
    var value=encodeURIComponent(document.getElementById('gcno1').value);
    var parameters="param1="+value;

    xmlhttp.open("POST", 'fetch_gc.php', true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send(parameters);
    xmlhttp.onreadystatechange=function(){
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200) 
        {
            var detail=xmlhttp.responseText.split('+');
            alert(detail[0]);
            document.getElementsByName('date1').value=String(detail[0]);
            alert("life " + document.getElementById('gcno1').value);

        }
    }

The alert inside the AJAX shows the correct response text, detail[0] but is unable to put the value in corresponding textbox i.e. with name 'gcno1'......

Please help me with this problem...

+1  A: 

.getElementsByName() returns a list, so:

document.getElementsByName('date1')[0].value=String(detail[0]);
jholster
any specific reason for that..?
Anurag
and what about .getElementsById...? does this also return a list..?
Anurag
also any idea to get the Id of an element through its object...?
Anurag
There can be unlimited number of elements with the same name, tag, etc, thus the methods are named getElementsByName(), getElementsByTagName() (note the plural form of _element_) and a list must be returned. Element's id must be unique, so single object is always returned by getElementById(). There is no such a function as getElementsById().
jholster
object.id or object.getAttribute('id') gives you the id of an object.
jholster