Elham Bagheri
Elham Bagheri

Reputation: 335

display value of button in textbox in javascript

I use bootstrap and I want to display my value of button in textbox when I clicked it and my textbox show my value of button but its value doesn't remain and textbox become empty early

<input type="submit" value="5000t "class="btn btn-lg btn-block btn-danger" id="btn1" onclick="myf()" >
<input type="text" class="form-control" id="txt" name="txt">

 function myf() {
     var txt = document.getElementById('txt');
     var btn1 = document.getElementById('btn1');
     txt.value = btn1.value;

 }

Upvotes: 0

Views: 306

Answers (2)

Prashank
Prashank

Reputation: 1

Above code is working fine ..... but while integrating it with your code it make be possible page is getting refreshed or submit button is triggering a postback which makes the textbox empty.

One thing which cab be done is instead of type as "submit" normally use button

Upvotes: 0

Andrea Pegoretti
Andrea Pegoretti

Reputation: 306

You should not to use a "submit" input type, which actually posts the form (as zinned pointed out), but a simple button, like:

<input type="button" value="5000t "class="btn btn-lg btn-block btn-danger" id="btn1" onclick="myf()" >

Of course you will need another submit button to send the form.

Upvotes: 3

Related Questions