0

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;

 }

5
  • 5
    Your submit triggers a postback which reloads the page, after which your textbox is empty again. Commented Aug 11, 2014 at 9:37
  • its working fine for me see the working example on jsfiddle:-jsfiddle.net/BtkCf/170 Commented Aug 11, 2014 at 9:39
  • @ZiNNED only if it is inside a form…. Commented Aug 11, 2014 at 9:40
  • hey are you using form tag or not? Commented Aug 11, 2014 at 9:42
  • @TilwinJoy True, but without knowing the rest of the code and seeing the symptoms, it seems like the most logical explanation. Commented Aug 11, 2014 at 9:44

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.