Home > OS >  How do I close a bootstrap (5.2) Modal from Javascript?
How do I close a bootstrap (5.2) Modal from Javascript?

Time:12-28

For a school project's sake, I created a modal using bootstrap in html, inside of it there is a prompt that I must check from javascript, how do I close the Modal from javascript, so that in case the prompt is valid I can just save the changes and if it isn't I throw an exception? small note (Please without jQuery, I've seen a similar thread that had as a reply using this library, it's forbidden for the assignment)

this is my html code :

<div  id="bidModal" tabindex="-1" aria-labelledby="bidModal" aria-hidden="true">
        <div >
        <div >
            <div >
                <h1  id="bidModalLabel">Bid amount</h1>
                <button type="button"  data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div >
                <p  id="prompted">How much do you want to bet?</p>
                <div >
                    <input id="bAmount" type="text"  aria-label="Amount of bet">
                    <span >€</span>
                </div>
            </div>
            <div >
                <button type="button"  data-bs-dismiss="modal">Cancel bid</button>
                <button type="button" onClick="bid()" >Save bid</button>
            </div>
        </div>
        </div>
    </div>

this is my javascript code :

function bid(){
    let valueOfBid = document.getElementById("bAmount").value;
    if(isNaN(valueOfBid)){
        //Cancel the prompt
    }

    players[realPlayer].bet=valueOfBid;
    changeButtonState(false);
    theTimer();
}

CodePudding user response:

Please try like this. I suggest that you change isNaN(valueOfBid) to valueOfBid == "" before you add my code on your codebase.

function bid(){
    let valueOfBid = document.getElementById("bAmount").value;
    if(valueOfBid == ""){
      alert(1)
        //Cancel the prompt
      var myModalEl = document.getElementById('bidModal');
var modal = bootstrap.Modal.getInstance(myModalEl)
modal.hide();
    }

    // players[realPlayer].bet=valueOfBid;
    // changeButtonState(false);
}
  • Related