0

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/

3 Answers 3

2
        $(function () {
            $('#Year').change(function () {
               $("#Yeartxt").val($("#Year").val());
               $("#readonlytxt").val($("#readonly").val());
            });
        });
Sign up to request clarification or add additional context in comments.

Comments

1
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.

2 Comments

I've been looking at this for almost an hour and cant believe I didn't see that. Thanks
@johnmadrak has a good, more simplified solution. If you find it hard to read, it still may be worth either using jQuery or JavaScript instead of a hybrid! That'll also help make spotting this error more easy :)
0

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();
 */

        });
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.