1

I'm having trouble changing a button value in bootstrap. I can change it using jQuery, but i'm changing it from a modal dialog. I looked elsewhere on SO but I couldn't find anything that seemed to match my specific issue?

Steps:

Click button. Change button text on main html form. Upon clicking the button it changes the text, closes the modal, and then immediately the text changes back to what it was originally. It should just change the text and stay that way, obviously.

$("#validate-rv-button").click(function () {
    $("#review-history-validate").val("Review History");
});

HTML

 <input id="review-history-validate" type="button" class="rvButtons btn btn-outline-warning btn-sm" data-toggle="modal" data-target="#review-history" value="Validate" />

Any help would be much appreciated.

2
  • 1
    can you post more associated code? what does the modal close callback/event look like? if I'm understanding "Upon clicking the button it changes the text, closes the modal, and then immediately the text changes back to what it was originally" it sounds like you're either re-instantiating your button or unintentionally undoing your changes. Also I would use .text() in place of .val() the value attribute is different than the actual text the user sees on the button. Commented Feb 20, 2018 at 21:59
  • 1
    Post all of the relevant code. Commented Feb 21, 2018 at 13:05

2 Answers 2

1

Another way to do it with <button></button>

 <button id="review-history-validate" type="button" class="rvButtons btn btn-outline-warning btn-sm" data-toggle="modal" data-target="#review-history" />Validate</button>

in jquery:

$("#validate-rv-button").click(function () {
    $("#review-history-validate").text("Review History");
});
7
  • I also tried .text() but that doesn't work either. It does less than .val(). .Val at least changed it, but then it changes back right after.
    – JasonG
    Commented Feb 20, 2018 at 17:06
  • Make sure that you use <button> instead of <input>. It should work, if not I misunderstood your post
    – Lemayzeur
    Commented Feb 20, 2018 at 17:12
  • I went ahead and changed it from <input> to <button>, but I get the same result when using .html() as I did with the input tag.
    – JasonG
    Commented Feb 20, 2018 at 17:17
  • Now try to change it when the modal is opened with shown.bs.modal. and change it back when the modal is closed with hidden.bs.modal
    – Lemayzeur
    Commented Feb 20, 2018 at 17:19
  • I have two buttons on my modal dialog. One attached to a form, the other closes the modal. When i click the close button, it changes like expected. When I click the one attached to the form, it doesn't do what is expected. Using the hidden/shown events changes the value, but only if I use the close button instead of the validate button. If I can get it the form submit to stop closing the modal it may work. But I don't know how to stop it from closing the dialog.
    – JasonG
    Commented Feb 20, 2018 at 17:50
0

I believe you got the naming wrong.

This works:

$("#review-history-validate").click(function () {
    document.getElementById("review-history-validate").value = "My value";
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="review-history-validate" type="button" class="rvButtons btn btn-outline-warning btn-sm" data-toggle="modal" data-target="#review-history" value="elo" />

Or jQuery only as your question:

$("#review-history-validate").click(function () {
    $("#review-history-validate").val("My value");
});
1
  • The initial jquery selector is another button in the html dialog box. I guess I should have mentioned that. that's why the id is different. So you click the button to update a form in the modal dialog, and when it is done, it should change a button elsewhere.
    – JasonG
    Commented Feb 20, 2018 at 17:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.