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;
<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.
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.
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):
in titles remove .value and insert it into the alert function.. and insert htis code in page head section..