SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Very Strange undefined ID issue

    Hello,

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

    form.php
    Code:
    <td><div id="test"><? echo $project;?></div></td>
    ajax.js
    Code:
    var test=document.getElementById("test").value;
    alert(test);
    out put is 'undefined'

    form.php
    Code:
    <td><input id="test" name="" type="text" value="<? echo $project;?>" /></td>
    ajax.js
    Code:
    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.

    Last edited by afridy; Jan 5, 2014 at 02:36. Reason: small mistake

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    The first example evaluates to this:

    Code:
    <td><div id="test">G4</div></td>
    You can get the text G4 with innerHTML

    Code:
    var test=document.getElementById("test").innerHTML;
    alert(test);
    This fails:

    Code:
    var test=document.getElementById("test").value;
    alert(test);
    because the test div has no value attribute.

    Whereas the second snippet evaluates to:

    Code:
    <td><input id="test" name="" type="text" value="G4" /></td>
    Therefore this works:

    Code:
    var test=document.getElementById("test").value;
    alert(test);
    as the test div has a value attribute.

  3. #3
    SitePoint Guru afridy's Avatar
    Join Date
    Mar 2007
    Posts
    966
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    Hi there,

    The first example evaluates to this:

    Code:
    <td><div id="test">G4</div></td>
    You can get the text G4 with innerHTML

    Code:
    var test=document.getElementById("test").innerHTML;
    alert(test);
    This fails:

    Code:
    var test=document.getElementById("test").value;
    alert(test);
    because the test div has no value attribute.

    Whereas the second snippet evaluates to:

    Code:
    <td><input id="test" name="" type="text" value="G4" /></td>
    Therefore this works:

    Code:
    var test=document.getElementById("test").value;
    alert(test);
    as the test div has a value attribute.
    Well understood sir!!!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •