Reputation: 3
I'm trying to use Jquery to pass the value of one box that already has value defined to another that does not. I tried the code but I keep getting 'undefined' in the text box instead of the text.
<input type="text" ID="readonly" runat="server" class="threeline-two" readonly=
"readonly" value="[email protected]" />
<input type="text" ID="readonlytxt" runat="server" readonly = "readonly" />
<input type="text" ID="Year" runat="server" class="threeline-two" />
<input type="text" ID="Yeartxt" runat="server" />
<script type="text/javascript">
$(function () {
$('#Year').change(function () {
var TxtBox = document.getElementById("Yeartxt");
TxtBox.value = $(this).val();
var string = $(this).val()
var TxtBox2 = document.getElementById("readonlytxt");
TxtBox2.value = $("readonly").val();
var string = $("readonly").val()
});
});
</script>
Upvotes: 0
Views: 423
Reputation: 825
$(function () {
$('#Year').change(function () {
$("#Yeartxt").val($("#Year").val());
$("#readonlytxt").val($("#readonly").val());
});
});
Upvotes: 2
Reputation: 5213
Replace the script with this:
$(function () {
$('#Year').change(function () {
var TxtBox = document.getElementById("Yeartxt");
TxtBox.value = $(this).val();
var string = $(this).val();
$("#readonlytxt").val($("#readonly").val());
/*
var TxtBox2 = document.getElementById("readonlytxt");
//$("readonly") selects no element: "#" is missing
TxtBox2.value = $("readonly").val();
*/
});
});
Upvotes: 0
Reputation: 3141
TxtBox2.value = $("#readonly").val();
is what was needed. JQuery requires the # to indicate its an ID as opposed to class or element tag. It works like a CSS selector. More on JQuery Selectors here.
See this jsfiddle for fixed code.
Upvotes: 1