0

This is my code: i want change value of city when click button

function add()
{
        $("#city").attr("value","abc");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type='text' id='city' value="123"/>
<input type="button" id='btnChange' value="Change" onclick="add();return false;"/>
    

If after Page load , i click button ,it working ok: city =abc.

But if after Page Load, i select and delete text "123", i click button , it not working. city=""

Why can't set value of input text by jquery after delete text?

saAction
  • 2,035
  • 1
  • 13
  • 18
D T
  • 3,522
  • 7
  • 45
  • 89

1 Answers1

2

use jquery val instead of attr

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='text' id='city' value="123" />
<input type="button" id='btnChange' value="Change" onclick="add();return false;" />
<script>
  function add() {
    $("#city").val("abc");
  }
</script>
Azad
  • 5,144
  • 4
  • 28
  • 56