user2624315
user2624315

Reputation: 327

How to set the value of textbox using jquery?

here is my code.

<form action="demo_form.asp">
  First name: <input type="text" name="fname" id="fname"><br>
  Last name: <input type="text" name="lname" id="lname"><br>
  <input type="submit" value="Submit">
</form> 

Javascript:

$(document).ready(function() {              

   $('#cash_received').change(function () {
       $("#lname").val("mytest");
   }
   });
});

It does not set the value mytest in the Last Name text box.

Upvotes: 1

Views: 79

Answers (2)

Tats_innit
Tats_innit

Reputation: 34107

Demo http://jsfiddle.net/VAcKN/

few things jsut as idea: (these are mere ideas yo) :) PSL nailed it anyways!

  • How about .keyPress.
  • End tag the input and chuck in value=""

Code

$(document).ready(function () {

    $('#fname').change(function () {
        $("#lname").val("mytest");
    });
});

Upvotes: 3

PSL
PSL

Reputation: 123739

That is because you have a syntax error in your script and of course your event did not get registered because of that.

$(document).ready(function () {

    $('#cash_received').change(function () {
        $("#lname").val("mytest");
    } //<-- Remove this
    });
});

Fiddle

Always look at your browser console for any errors, you can catch up all the syntax errors then and there itself

Upvotes: 4

Related Questions