tags:

views:

70

answers:

3

Hi, i'm trying to use jquery to change the value of an input text box, but it's not working, here is my code...

    <form method="get">
            <input id="temp" type="text" name="here" value="temp value" />
            <input class="form" type="radio" value="1" name="someForm" /> Enter something
            Here: <input id="input" type="text" name="here" value="test value" />
    </form>

    <script>

    $(document).ready(function () {
        $('.form').click(function () {
            var newvalue = $("#input").val();
            $("#temp").val(newvalue);
            $("#input").val($("#temp").val());
        });
    });

</script>

the value of the "#input" text box is not changing!!!!!! why is that??? what am i missing??? thanks a 10 million in advance p.s. the value of the text box is changing, but the value attribute of the input is NOT!!! even if i write...

var value = $("#temp").val();
$("#input").attr("value", value);
+1  A: 

You are selecting input not #input so you are setting the value of the first <input> not the element with id="input".

The first input is #temp so you are setting the value of that input to its current value.

David Dorward
A: 

Try:

$("#input").val($('#temp').val());

instead of:

$("input").val($(temp).val());
Sarfraz
ok, the text value is changing, what about the "value" attribute, it will not change!!!!even if i write...var x = $("temp").val();("#input").attr("value", "x");
Lina
+4  A: 

The value is not changing because you assign the value that #input already has.


Have a closer look:

var newvalue = $("#input").val();
$("#temp").val(newvalue);
$("#input").val($("#temp").val());
  1. Let value of #input be foo.
  2. You assign newvalue = value of #input = 'foo'
  3. You set the value of #temp:
    value of #temp = newvalue = value of #input = 'foo'
  4. You set the value of #input:
    value of #input = value of #temp = newvalue = value of #input = 'foo'

So depending on which field should get which value you have to change this ;)

Felix Kling
That was the problem in the logic.. previously Lina was asking about this syntax..$("input").val($(temp).val());So now u r right...After correcting the proper syntax..!!!
piemesons