function printvalues() { document.write("This is my first JavaScr...">

Html form.input.value not printing why?

...

<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 does not print the value document.write(form.inputobj1.value);

+3
source share
4 answers

document.writeoverwrites the current document. After that, the whole element <form>disappears from the DOM and, therefore, it and its input elements cannot be found.

Replace document.write(...)with an example alert(...)and it should work.

Alternatively, you can write it as innerHTMLanother element. For instance.

<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>

, " ", , ... Javascript, jQuery. Javascript, DOM, , ;)

+3
document.write()

, , . . , , , document.write , , , .

+1

document.getElementById DOM. :

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

DOM:

<input id="inputobj1_id" name="inputobj1" value="123" />
0
<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

Source: https://habr.com/ru/post/1740404/


All Articles