Is the modal popup displayed in the center of the screen when the submit button is clicked?

My bootstrap modal popup is not working when I click the submit button. I can only use Bootstrap4, JS, and jQuery3.0.

JS Fiddle

function check() {
  var inputs = document.getElementsByTagName("input");
  var inputs = document.getElementsByTagName("select");
  var inputs = document.getElementsByTagName("radio");
  var filled = true;
  var oneChecked = false;

  for (var i = 0; i < inputs; i++) {
    if (inputs[i].type === "text" && !inputs[i].value) {
      filled = false;
    }

    if (inputs[i].type === "select" && inputs[i].value) {
      filled = false;
    }

    if (inputs[i].type === "radio" && inputs[i].checked) {
      oneChecked = true;
    }
  }
  if (!oneChecked) {
    filled = false;
  }

  for (var j = 0; j < inputs; j++) {
    if (!inputs[j].value) {
      filled = false;
    }
    if (!inputs[j].oneChecked) {
      filled = true;
    }
  }

  if (filled) {
    document.getElementById("submitacc").disabled = false;
  } else {
    document.getElementById("submitacc").disabled = true;
  }
}

window.addEventListener("keyup", check);
window.addEventListener("click", check);
<div>
  <form class="contactForm">
    <div class="row">
      <!-- From Account name -->
      <div class="form-group col-6" style="margin-bottom: 0px;">
        <label class="col-sm-6 control-label p-sm-0 acc-merge" for="frm_acc">Select the A/c from which the records are to be transferred:*</label>
        <select class="form-control select-data .has-success" name="frm_account" id="frm_acc" required>
          <option value="">Choose an items</option>
          <option value="1">Plumz</option>
          <option value="2">Plumz2</option>
          <option value="3">Plumz3</option>
          <option value="4">Plumz4</option>
          <option value="5">Plumz5</option>
        </select>
        <div class="input-group col-sm-6 p-sm-0 form-group"></div>
      </div>
      <!-- To account name -->
      <div class="form-group col-6" style="margin-bottom: 0px;">
        <label class="col-sm-6 control-label p-sm-0 acc-merge" for="to_acc">Select the A/c to which the records are to be transferred:*</label>
        <select class="form-control select-data .has-success" name="to_account" id="to_acc" required>
          <option value="">Choose an items</option>
          <option value="1">Plumz</option>
          <option value="2">Plumz2</option>
          <option value="3">Plumz3</option>
          <option value="4">Plumz4</option>
          <option value="5">Plumz5</option>
        </select>
        <div class="input-group col-sm-6 p-sm-0 form-group"></div>
      </div>
    </div>
    <div>

  </form>

  <!-- Submit Button -->

  <div class="form-group ml-auto mt-2 mb-0">
    <div class="col-md-12 stockform_submit" id="">
      <button type="submit" class="btn add-btn submit-btn disabled" data-toggle="modal" data-target="#merge_err" disabled>Submit</button>
      <button type="reset" class="btn btn-default reset-btn stock_rst" style="left: 0%" id="reset-btn">Reset</button>
    </div>
  </div>

  <!-- modal -->

  <div class="modal fade" id="merge_err">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title">Error..</h4>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          <p class="popup-text">Merging option Faild in Transaction record TXN#:XXXXXXX</p>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-danger ml-10 pull-right okbutton" id="ok" data-dismiss="modal">Ok</button>
          <!-- onclick="window.location.href='accmaster.html'" -->
        </div>
      </div>
    </div>
  </div>

I am just a beginner in coding and struggling with this code. Please help me make it work.

Is there any other method?

Example here:

  [1]: https://jsfiddle.net/joelshah/vo0qcs6h/

Here is my example link

I want the submit button to be enabled when the form is filled, and after clicking the submit button, the modal popup should show.

Answer №1

<div class="container">
  <div class="row">
    <div class="col-md-12>
      <form class="contactForm">
        <div class="form-group col-6" style="margin-bottom: 0px;">
          <label class="col-sm-6 control-label p-sm-0 acc-merge" for="frm_acc">Select the A/c from which the records are to be transferred:*</label>
          <select class="form-control select-data .has-success" name="frm_account" id="frm_acc" required>
            <option value="">Choose an item</option>
            <option value="1">Plumz</option>
            <option value="2">Plumz2</option>
            <option value="3">>Plumz3</option>
            <option value="4">Plumz4</option>
            <option value="5">>Plumz5</option>
          </select>
          <div class="input-group col-sm-6 p-sm-0 form-group"></div>
        </div>
        
        <div class="form-group col-6" style="margin-bottom: 0px;">
          <label class="col-sm-6 control-label p-sm-0 acc-merge" for="to_acc">Select the A/c to which the records are to be transferred:*</label>
          <select class="form-control select-data .has-success" name="to_account" id="to_acc" required>
            <option value="">Choose an item</option>
            <option value="1">Plumz</option>
            <option value="2">Plumz2</option>
            <option value="3">>Plumz3</option>
            <option value="4">Plumz4</option>
            <option value="5">>Plumz5</option>
          </select>
          <div class="input-group col-sm-6 p-sm-0 form-group"></div>
        </div>

        <button type="button" class="btn add-btn submit-btn" data-toggle="modal" data-target="#merge_err">Submit</button>
        <button type="reset" class="btn btn-default reset-btn stock_rst" style="left: 0%" id="reset-btn">Reset</button>
      </form>
    </div>
  <div>
</div>
    

    <div class="modal" id="merge_err">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <h4 class="modal-title">Error..</h4>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            <p class="popup-text">Merging option Faild in Transaction record TXN#:XXXXXXX</p>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-danger ml-10 pull-right okbutton" id="ok" data-dismiss="modal">Ok</button>
          </div>
        </div>
      </div>
    </div>

Implementing JQuery instead of lengthy JavaScript code:

JQuery:

$(document).ready(function(){


 // Disabling the submit button by default
  $('.submit-btn').attr('disabled', true);
  $('select').on('change', function() {
    if($('#frm_acc').val() > 0 && $('#to_acc').val() > 0) {
      $('.submit-btn').attr('disabled', false);
    }
  });

  $('.reset-btn').on('click', function() {
    $('.submit-btn').attr('disabled', true);
  });

});

Answer №2

Using vanilla JavaScript instead of jQuery is a puzzling choice.

However, the function check() contains numerous errors. One major mistake is that the input variable gets overwritten every time getElementsByTagName is called. It seems like you want to collect all input, select, and radio elements, check if they have values, and update the filled variable accordingly.

By using the ES6 spread operator and forEach function, you can loop through the array of elements returned by getElementsByTagName and check for inputs with values.

Please refer to the code snippet provided below:

function check() {
  var inputs = [...document.getElementsByTagName("input"), ...document.getElementsByTagName("select"), ...document.getElementsByTagName("radio")];

  var filled = false;
  var numberOfFilledInputs = 0;

  inputs.forEach((input) => {
    if (!!input.value) numberOfFilledInputs++;
  });

  if (numberOfFilledInputs === inputs.length) {
    document.getElementById("submitacc").disabled = false;
  } else {
    document.getElementById("submitacc").disabled = true;
  }
}

window.addEventListener("keyup", check);
window.addEventListener("click", check);

The above code demonstrates how to accurately handle input validation in your form.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to prevent the hover state from activating when the mouse is in the corner of a circular QPushButton

I'm working on customizing a QPushButton that has round corners: https://i.stack.imgur.com/g8zfs.png When the mouse hovers over the button, I want to change its style: https://i.stack.imgur.com/SDm79.png To achieve this, I utilized setStyleSheet f ...

Determine a person's vertical measurement with jQuery

Is there a way to determine the distance from the top of the page to where a link has been inserted? For example, we can use the following code snippet to calculate the height of the window: w = $(window).height(); I am interested in finding out how to ...

How to personalize cell and border spacing within the <table> HTML element

Looking to create a design similar to this https://i.sstatic.net/qONUa.png I attempted using border-spacing, but it applies between all cells. Is there a way to eliminate the space only between label and text cells? Or would using divs for the entire str ...

"I aim to ensure that my HTML, CSS, and JavaScript project is designed to be highly responsive

Bootstrap implementation on my project is causing issues with mobile view and the positioning of my divs. Take a look at my code below: table { border-collapse: collapse; margin: auto; position: absolute; width: 60%; height: 60%; ...

The ng-style directive is not functioning properly

After selecting a category, I attempted to change the color using "ng-style" in my HTML code. However, it seems that the color change is not taking effect. This is the snippet from my HTML code: <div ng-repeat="item in ListExpense" ng-class-odd="&apos ...

Loop through an array of div IDs and update their CSS styles individually

How can I iterate through an array of Div IDs and change their CSS (background color) one after the other instead of all at once? I have tried looping through the array, but the CSS is applied simultaneously to all the divs. Any tips on how to delay the ...

Refreshing the images array in the DOM using ajax technology

I have a webpage with various images scattered under a table, like in the example of homepage.htm: <table id="imagesTable"> <tr> <td> <img src="img1.png"> <div> <img src= ...

Show child element when hovering over parent element

My current issue involves a button animation I am attempting to create. I envision a description box smoothly sliding out from behind a button whenever it is hovered over. However, my current implementation lacks the desired transition effect and simply ju ...

Creating designs to cater to outdated web browsers, prioritizing backward compatibility

I am looking to synchronize my design for older browsers like IE7 and above. I have designed the layout specifically for these browsers, and after researching, I found a tool that uses Javascript to implement the code. Is there an alternative method to ach ...

Run a query if the textarea includes this specific text

Check out the code below: $(document).ready(function () { if ($("#write_text_id").text() === "Saurav & Kunal Are God") { $(".submit_form_field").addClass('item-3'); } }); <script src="https://cdnjs.cloudflare.com/ajax/lib ...

Can you provide guidance on configuring the image class within a DOM element using echo?

What is the method to set the class attribute of an img element using echo function? <div class="allnis" style="padding: 15px; text-transform: uparcase;"> <?php foreach($tv_id->networks as $prod){ echo "<img val ...

Fetch information from MySQL, create a new row for each data entry

Currently, I am working on a project for my school that involves retrieving student works from a database. For the homepage of my project, I have set up 10 divs to hold the data returned from a query. The reason I preset these divs is because I only need ...

Center the input field both vertically and horizontally within an overlay

When the user clicks on the search icon, I want to display the search field as an overlay. Currently, the overlay is working but the search input field is appearing in the middle vertically. Check out the Fiddle <a href="#overlay" id="open-overlay"&g ...

I'm confused about why the PHP contact form is displaying an error message instead of sending the message

Hello there, I'm having trouble understanding why I'm receiving a message about not having permission to use a script from another URL when I fill out this form. The expected behavior would be for the form to thank me and then proceed to send a ...

Initial Blazor Navbar Display Issue

I'm currently working on a Blazor learning project and encountered an issue with the navbar after installing Blazorise. The navbar is not showing up when I start the web app, but if I minimize Chrome, the navbar-toggler-icon appears. Clicking on the i ...

The animation feature in Angular JS seems to be malfunctioning

Currently in the process of creating a slideshow using AngularJS, similar to the one found at this link, which includes navigation arrows (next and prev). Here is the HTML code snippet: <div class="carousel"> <div class="left"><input ty ...

Adjust the canvas size when switching in and out of fullscreen mode

Is there a way to dynamically resize the canvas when entering and leaving fullscreen mode using three.js and its THREE.WebGLRenderer? I have tried implementing the following code, but it seems to set the window dimensions "one iteration back". For instanc ...

Tips for customizing the icon used for expanding rows in a PrimeNG datatable

I have implemented PrimeNG datatable in my project. Here is the code snippet I am using: <p-dataTable [value]="alerts" [expandableRows]="true" [expandedRows]="expandedItems" #dt> <p-column expander="true"></p-column> </p-dataTable> ...

Styling the <div> element to create a unique rotation and tilt aesthetic

Would anyone be able to guide me on how to recreate the tilted style of the header section shown in the image using HTML and CSS? https://i.sstatic.net/mhJWA.png body { margin: 0px; background-color: #FFEF4C; } #header { background-color: #FF35 ...

Can anyone provide guidance on how to display all the value= options within an HTML (aspx) page?

I'm currently working with a .NET website that utilizes multiple webforms. Users input their data through a browser, and the submitted information is stored in a SQL Server database. However, I'm facing difficulty analyzing the data because ther ...