Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net Set Textbox 1 to Equal Textbox 2

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!

like image 914
aron Avatar asked Dec 10 '22 15:12

aron


2 Answers

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;
like image 98
Cᴏʀʏ Avatar answered Dec 21 '22 23:12

Cᴏʀʏ


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
like image 41
Matt Weldon Avatar answered Dec 21 '22 22:12

Matt Weldon