How to change input field value using jquery?

I am trying to use jquery to change the value of an input text field, but it does not work, 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 text field "#input" does not change !!!!!! Why is this??? what am I missing ??? thanks to 10 million postscript in advance, the value of the text field is changing, but the input value attribute is NOT! even if I write ...

var value = $("#temp").val();
$("#input").attr("value", value);
+3
source share
3 answers

The value does not change, because you are assigning a value that it #inputalready has.


Take a closer look:

var newvalue = $("#input").val();
$("#temp").val(newvalue);
$("#input").val($("#temp").val());
  • Let the value #inputbe foo.
  • You appoint newvalue = value of #input = 'foo'
  • You set the value #temp:
    value of #temp = newvalue = value of #input = 'foo'
  • You set the value #input:
    value of #input = value of #temp = newvalue = value of #input = 'foo'

, , , ;)

+6

input not #input, <input> id="input".

#temp, .

+2

Try:

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

instead:

$("input").val($(temp).val());
+1
source

Source: https://habr.com/ru/post/1742978/


All Articles