getElementById problem

Discussion in 'JavaScript' started by SHOwnsYou, Jan 15, 2010.

  1. #1
    I am trying to get the value of input elements on my page with javascript, but it isn't working.

    This is my page:

    
    <script type="text/javascript">
    var titles = document.getElementById('title').value;
    alert(titles);
    </script>
    
    <div id="adminSetup">
    <form>
    <input id="title" name="title" value="Test Title" />
    </form>
    </div>
    
    Code (markup):
    I also tried document.adminSetup.getElementById('title').value;
     
    SHOwnsYou, Jan 15, 2010 IP
  2. Stomme poes

    Stomme poes Peon

    Messages:
    3,195
    Likes Received:
    136
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <script type="text/javascript">
    var titles = document.getElementById("title");
    alert(titles.value);
    </script>


    if you had
    <div>
    <form id="adminSetup" (and all the other stuff you're supposed to have here)>
    <input id="title" name="title" value="Test Title" />
    </form>
    </div>
    you could

    <script type="text/javascript">
    var titles = document.forms["adminSetup"].elements["title"];
    alert(titles.value);
    </script>

    I'm not sure if
    var titles = document.forms["adminSetup"].elements["title"].value;
    would work.

    You'd first need to define adminSetup to do that.
     
    Stomme poes, Jan 15, 2010 IP
  3. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #3
    place the script at the bottom (not at the top) of the HTML. You would be getting an error because the elements have not been loaded yet.
     
    camjohnson95, Jan 15, 2010 IP
  4. spiderman3

    spiderman3 Peon

    Messages:
    177
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    place this code in your page head section.
    <script type="text/javascript">
    var titles = document.getElementById('title').value;
    alert(titles);
    </script>
    Code (markup):
    place this code in body section, and create an event, just like click, double click etc
    <div id="adminSetup">
    <form>
    <input id="title" name="title" value="Test Title" like this onclick="this.run();"/>
    </form>
    </div>
    Code (markup):
     
    spiderman3, Jan 29, 2010 IP
  5. nehakv

    nehakv Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    in titles remove .value and insert it into the alert function..
    and insert htis code in page head section..
     
    nehakv, Feb 4, 2010 IP