Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot access element id's in bootstrap modal using jquery, getting undefined error

Jquery and Bootstrap modalbox code is in same page lets say index.php

<div class="modal fade" id="tileModal" tabindex="-1" role="dialog" aria- 
                    labelledby="mytileModal" aria-hidden="true">
          <div class="modal-dialog">
            <div class="modal-content">
              <div class="modal-header"><button type="button" class="close" 
data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">Select Tiles</h4>
              </div>
              <div class="modal-body">
               <div id="result"> </div>
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary" id="saveChange">Save changes</button>
              </div>
            </div>
          </div>
        </div>

Note: DIV is getting ajax response html and elements are drawn dynamically. And after the elements are drawn in this case checkbox . On saveChange button click on modal I want to get checkbox value.

$("#saveChange").click(function () {
    var midVal = $('#test1').val();
    console.log(midVal);
});

Getting undefined

Not Sure why I cannot access modal elements from the same page. However Im able to access elements from other DIV outside modal

like image 593
amd Avatar asked Jun 29 '15 14:06

amd


People also ask

How can check Bootstrap modal is open or not in jQuery?

How check modal is open or not in jQuery? You can simply use the modal('show') method to show or open the modal window in Bootstrap using jQuery. Other related Bootstrap's modal methods are modal('hide') and modal('toggle') .

How do I pass data to modal popup?

Data can be passed to the modal body from the HTML document which gets displayed when the modal pops up. To pass data into the modal body jquery methods are used. jQuery is similar to JavaScript, however jQuery methods are simple and easier to implement. jQuery reduces the lines of code.

What is modal in jQuery?

JQuery Modal is an overlay dialog box or in other words, a popup window that is made to display on the top or 'overlayed' on the current page. It is a pop up modal box that is converted into the viewport deliberately for the user to interact with before he can return to the actual site.

How do you know if a modal is open or closed?

on('show. bs. modal', function (e) { console. log('modal opened'); // Your code goes here });


2 Answers

Attach your event to a container instead to the element directly. Also use on('click', ... instead of click directly.

$("#tileModal").on('click', '#saveChange', function () {
    var midVal = $('#test1').val();
    console.log(midVal);
});
like image 82
viarnes Avatar answered Oct 27 '22 05:10

viarnes


My solution to check if the check box was checked or not was simple. I just created class and was able to easily check if checkbox was checked or not in the bootstrap modal

$('.className').is(':checked'); // works $('#elementId').is(':checked'); // gives false always

like image 40
amd Avatar answered Oct 27 '22 07:10

amd