Asp.net Set text box 1 to equal text box 2

What is a clean way, like 1 line of JavaScript code, to set one text property of a text to another?

eg. JavaScript way to do this:

txtShipCity.Text = txtCity.Text; 

Thanks!

+4
source share
2 answers

In JavaScript:

 document.getElementById('txtShipCity').value = document.getElementById('txtCity').value; 

Sweeten it with jQuery:

 $('#txtShipCity').val($('#txtCity').val()); 

Although you may have to use the ClientID two text fields, so your JS might look pretty unpleasant, for example:

 document.getElementById('<%= txtShipCity.ClientID %>').value = document.getElementById('<%= txtCity.ClientID %>').value; 
+9
source

By providing you with id attributes in text fields, you can easily have a single liner in jQuery by doing the following:

$("#txtShipCity").text($("#txtCity").text()); (or $("#txtShipCity").val($("#txtCity").val()); if you are dealing with input )

If jQuery is actually not an option, try

 document.getElementById("txtShipCity").value = document.getElementById("txtCity").value 
+5
source

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


All Articles