最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - html form.input.value is not getting printed why? - Stack Overflow

programmeradmin2浏览0评论

...

<script type="text/javascript">
 function printvalues() {
  document.write("This is my first JavaScript!");
  document.write(form.inputobj1.value);
  document.write(form.inputobj2.value);
 }
</script>
<form name="form">
 <input name="inputobj1" value="123" />
 <input name="inputobj2" value="abc"/>
 <input type="button" onclick =" printvalues();"> 
</form>

why this line is not printing the value document.write(form.inputobj1.value);

...

<script type="text/javascript">
 function printvalues() {
  document.write("This is my first JavaScript!");
  document.write(form.inputobj1.value);
  document.write(form.inputobj2.value);
 }
</script>
<form name="form">
 <input name="inputobj1" value="123" />
 <input name="inputobj2" value="abc"/>
 <input type="button" onclick =" printvalues();"> 
</form>

why this line is not printing the value document.write(form.inputobj1.value);

Share Improve this question edited Apr 9, 2010 at 2:02 Justin Ethier 134k52 gold badges232 silver badges287 bronze badges asked Apr 9, 2010 at 1:31 chanduchandu 311 silver badge3 bronze badges 3
  • What does it do? What's the full HTML & Javascript code? Do any errors appear in the error console? – Matchu Commented Apr 9, 2010 at 1:40
  • just i am learning html. i have posted html but it is not displaying in stackoverflow. this gives inputobj1 is not an object – chandu Commented Apr 9, 2010 at 1:51
  • i call the printvalues() from a form that has two input objects named inputobj1 and inputobj2. while clicking a button from the form am calling the function – chandu Commented Apr 9, 2010 at 1:55
Add a ment  | 

4 Answers 4

Reset to default 3

The document.write overwrites the current document. Once done that, the whole <form> element disappears from the DOM and hence it and its input elements cannot be found.

Replace document.write(...) by for example alert(...) and it should work.

Alternatively you can write it as innerHTML of another element. E.g.

<script type="text/javascript">
    function printvalues() {
        var div = document.getElementById("divId");
        div.innerHTML += "This is my first JavaScript!";
        div.innerHTML += form.inputobj1.value;
        div.innerHTML += form.inputobj2.value;
    }
</script>
<form name="form">
    <input name="inputobj1" value="123" />
    <input name="inputobj2" value="abc"/>
    <input type="button" onclick =" printvalues();"> 
</form>
<div id="divId"></div>

Note that this is not the "best practice", but since you're learning... When done with core Javascript, I remend you to get yourself through jQuery. It's a Javascript library which greatly eases DOM manipulation like that and more ;)

document.write()

is probably not what you want. It will overwrite the entire contents of the page. The reason you're getting that error is because when you call document.write, it removes all the previous content, and thus the page will no longer have a form element.

Normally you would use a function such as document.getElementById to get a DOM element. For example:

alert( document.getElementById('inputobj1_id').value );

For DOM element:

<input id="inputobj1_id" name="inputobj1" value="123" />
<script type="text/javascript">
function printvalues() {
var x = document.form.inputobj1.value;
var y = document.form.inputobj2.value
document.write("<Html><head></head><body><h1>");
document.write("This is my first JavaScript!</h1></br><h3>");
document.write(x);document.write("</h3></br><h3>");
document.write(y);document.write("</h3></body></html>");
}
</script>
<form name="form">
<input name="inputobj1" value="123" />
<input name="inputobj2" value="abc"/>
<input type="button" value="click" onclick =" printvalues();"> 
</form>
发布评论

评论列表(0)

  1. 暂无评论