How to get data values ​​in bootstrap module using jQuery or javascript when clicking a button inside a modal?

I want to get the data values ​​of the boot modal when I click the 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 this is how I pass data 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>

A modal can be called from any of many buttons, and I need to get only the data identifier of the corresponding button. For example, if I click Cancel 1, I should get the data identifier as 1 after clicking Yes in modal mode.

I want to get the modal data value in the "id" field, which is 1234 when I click the "Yes" button in this modal using jQuery or javascript.

+4
3

: http://jsfiddle.net/exr00e0d/

href. ().

$('.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);
});
+4

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> 
+1

In jQuery

$('#savebutton').click(function() {
    var id = $('#link').data('id');
    // if for some reason, the code above doesn't work,
    // $('#link').attr('data-id');
    console.log(id);
});

Edit:

Looking at your jsfiddle

$('#link').click(function() {
    // we want to copy the 'id' from the button to the modal
    var target = $(this).attr('target');
    var id = $(this).data('id');

    $(target).data('id', id);
});

$('#savebutton').click(function() {
    // now we grab it from the modal
    var id = $('#addBookDialog').data('id');

    console.log(id);
});
+1
source

Source: https://habr.com/ru/post/1658081/


All Articles