Subham Sb Subham Sb - 6 months ago 17
HTML Question

please help me in this onclick function for button in js

I wrote this code to replace the element in div:"change" with the input given in textbox:"use10" when the button:"insert" is clicked.But when i click nothing
is happening please help.Here is my code:

<div id="change">
<?php
echo $r9['fname'];
echo "&nbsp;";
echo $r9['lname'];
echo "&nbsp;";
?>
</div>

<div id="dialog" title="Change Details">
<form method="POST" name="changename">
<input type="text" id="use10" name="use10" placeholder="First Name">
<input type="text" id="use20" name="use20" placeholder="Last Name">
<button type="button" type="submit" name="insert" id="insert" class="btn btn-success">Change</button>
<script>
document.getElementById("insert").onclick=function(){
var fn=getElememtById("use10").value;
document.getElementById("change").innerHTML=fn;}
</script>
</form>
</div>

Answer

Try this:

Jsfiddle: https://jsfiddle.net/74Lmwcmt/

$( document ).ready(function() {  

    document.getElementById("insert").onclick=function(){
     var fn=document.getElementById("use10").value; // you forget to write "document." over here
     document.getElementById("change").innerHTML=fn; // give id of div
    }

});

OR

window.onload = function() { 

 document.getElementById("insert").onclick=function(){
         var fn=document.getElementById("use10").value; // you forget to write "document." over here
         document.getElementById("change").innerHTML=fn; // give id of div
        }
};
Comments