user1480978
user1480978

Reputation: 3

Set predefined value to another textbox

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>

http://jsfiddle.net/YAN2X/

Upvotes: 0

Views: 423

Answers (3)

johnmadrak
johnmadrak

Reputation: 825

        $(function () {
            $('#Year').change(function () {
               $("#Yeartxt").val($("#Year").val());
               $("#readonlytxt").val($("#readonly").val());
            });
        });

Upvotes: 2

Alberto De Caro
Alberto De Caro

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

jaypeagi
jaypeagi

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

Related Questions