Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get data values of a bootstrap modal using jQuery or javascript on clicking button inside the modal?

I want to get data values of a bootstrap modal on clicking button inside the modal. Here is my modal -

<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-dialog">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
                                    <h4 class="modal-title" id="myModalLabel">Cancel</h4>
                                </div>
                                <div class="modal-body"> 
                                    Are you sure you want to cancel this?
                                </div>
                                <div class="modal-footer">
                                    <button type="button" id="savebutton" class="btn btn-success" >Yes</button>  
                                    <button type="button" class="btn btn-danger" data-dismiss="modal">No</button>
                                </div>
                            </div>
                        </div> 
                    </div>

And here is the way I am passing the data to modal -

Update -

    <button data-target='#myModal' id='link' data-toggle='modal' data-id="1"  class='btn btn-danger cancel_modal' style='font-size:10px;padding:2px 5px;color:white;' >Cancel 1</button>
    <button data-target='#myModal' id='link' data-toggle='modal' data-id="2"  class='btn btn-danger cancel_modal' style='font-size:10px;padding:2px 5px;color:white;' >Cancel 2</button>
    <button data-target='#myModal' id='link' data-toggle='modal' data-id="3"  class='btn btn-danger cancel_modal' style='font-size:10px;padding:2px 5px;color:white;' >Cancel 3</button>
    ...
    ..
    .
    <button data-target='#myModal' id='link' data-toggle='modal' data-id="n"  class='btn btn-danger cancel_modal' style='font-size:10px;padding:2px 5px;color:white;' >Cancel n</button>

Modal can be called from any one of the many buttons and I need to get the data-id of the concerned button only. e.g.- if I click on "Cancel 1" button, I should get data-id as 1 after clicking "Yes" button in modal.

I want to get the modal data value of "id" field which is 1234 on clicking "Yes" button in this modal using jQuery or javascript.

like image 670
gauravparmar Avatar asked Oct 18 '16 11:10

gauravparmar


People also ask

How can I get Bootstrap table row values on clicking Bootstrap modal?

One approach would be to refresh the content of the modal every time a selection on a row happens. The selection can be handled when clicking the row and the refresh of the (possibly rich/complicated) content of the modal could be achieved by assigning a template to it and binding its rendering to a property.

How can I get dynamic data from Bootstrap modal?

Load Dynamic Content from Database in Bootstrap ModalBy clicking the Open Modal ( . openBtn ) button, the dynamic content is loaded from another PHP file ( getContent. php ) based on the ID and shows on the modal popup ( #myModal ).

How do you call a modal on a button click?

To trigger the modal window, you need to use a button or a link. Then include the two data-* attributes: data-toggle="modal" opens the modal window. data-target="#myModal" points to the id of the modal.

How do you go from modal to data?

The data entered the input fields are extracted using the ids of the respective fields using the jQuery val() method. Next the data obtained from the input fields are concatenated into a string. This string is passed to the modal body using the html() method of jQuery.


2 Answers

I have modifed your fiddle : http://jsfiddle.net/exr00e0d/

you had taken href. instead of that i took the target(modal).

$('.cancel_modal').click(function() {
   //alert('called');
    // we want to copy the 'id' from the button to the modal
    var href = $(this).data('target');
    var id = $(this).data('id');

    // since the value of href is what we want, so I just did it like so
    alert(href);
    // used it as the selector for the modal
    alert(id);
    $(href).data('id', id);
});
like image 137
Abdul Rehman Sayed Avatar answered Oct 26 '22 22:10

Abdul Rehman Sayed


try this

function getid() {
var id=document.getElementById('savebutton').getAttribute("data-‌​id");
return id;
}

<button type="button" id="savebutton" class="btn btn-success" onclick="getid()" >Yes</button> 
like image 23
user1844933 Avatar answered Oct 27 '22 00:10

user1844933