views:

87

answers:

2

Hello, What's the cleanest way, like 1 line of JavaScript code, to set one text box's text property to equal another?

e.g. the JavaScript way to accomplish this:

txtShipCity.Text = txtCity.Text;

Thanks!

+8  A: 

In JavaScript:

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

Sweeten it with jQuery:

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

Although you will probably have to use the ClientIDs of the two textboxes, so your JS might end up looking pretty nasty, like:

document.getElementById('<%= txtShipCity.ClientID %>').value = document.getElementById('<%= txtCity.ClientID %>').value;
Cory Larson
+1 for beating me to it and mentioning the potential need to use `ClientID` ;)
Matt Weldon
+5  A: 

Providing you have id attributes on the textboxs you could easily have a one-liner in jQuery doing the following:

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

If jQuery isn't really an option then try

document.getElementById("txtShipCity").value = document.getElementById("txtCity").value
Matt Weldon
I answered a minute before you but you get up-voted instead. Nice, SO users. ;) Correct answer though, so I might as well give you a +1.
Cory Larson