Button that your click action changes the value of a label, returns me ([object HTMLInputElement]), for something written in a text box

0
        function addLoadEvent() {  
    var nombre1 = document.getElementById("lbltipAddedComment");
    var nombre2 = document.getElementById("lbltipAddedComment2");
        nombre1 .innerHTML=nombre2 ;
};

       <html><body><label id="lbltipAddedComment">Nombre</label>
       <input type="text" id="lbltipAddedComment2">
       <button id="lbltipAddedComment3" onclick="addLoadEvent()"</button>

    
asked by Leon9091 20.11.2018 в 22:58
source

1 answer

0

The following function can help you, using JQuery you get the value of label and input by means of your id , you assign the value of input to label each time do click on the button:

function addLoadEvent() {  
    var nombre1 = $('#lbltipAddedComment').text();
    var nombre2 = $('#lbltipAddedComment2').val();
    
    $('#lbltipAddedComment').text(nombre2);
};

       
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
  <body>
     <label id="lbltipAddedComment">Nombre</label>
     <input type="text" id="lbltipAddedComment2">
     <button id="lbltipAddedComment3" onclick="addLoadEvent()">Click!</button>
   </body>
</html>

I commented to you that your code did not work because what you should get from label is the text by means of textContent and input you are not getting the valor by means of .value .

I'll leave the code without JQuery in case you need it:

function addLoadEvent() {  
    var nombre1 = document.getElementById("lbltipAddedComment").textContent;
    var nombre2 = document.getElementById("lbltipAddedComment2").value;
    
    document.getElementById("lbltipAddedComment").textContent = nombre2;
};
<html>
  <body>
     <label id="lbltipAddedComment">Nombre</label>
     <input type="text" id="lbltipAddedComment2">
     <button id="lbltipAddedComment3" onclick="addLoadEvent()">Click!</button>
   </body>
</html>

I hope it helps you. Greetings.

    
answered by 20.11.2018 / 23:58
source