1

I have this in JS and the code below. I want to send the data-id into modal and also when closing the modal to dismiss the data-id. how can I do that using javaScript???

fetch("https://data.uk/api/" + $searchValue).then(
            res => {
                res.json().then(
                    data => {
                        if (data.length > 0) {

                            var temp = "";
                            temp += "<thead>";
                            temp += "<tr>";
                            temp += "<th>ID</th>";
                            temp += "<th>Force Name</th>";
                            temp += "<th>Details</th>";
                            temp += "</tr>";
                            temp += "</thead>";
                            temp += "<tbody>";

                            data.forEach((itemData) => {
                                temp += "<tr><td>" + itemData.id + "</td>";
                                temp += "<td>" + itemData.name + "</td>";
                                temp += "<td><a href='#' data-id='" + itemData.id + "' data-toggle='modal' data-target='#myModal'>More Detail</a></td></tr>";
                            });
                            temp += "</tbody>";
                            document.getElementById('data').innerHTML = temp;
                        }
                    }
                )
            }
        )

and I want to get each data-id value into modal using this

$("myModal").on("show.bs.modal", function(e) {
    var b = $(e.relatedTarget);
    var r = b.data("id");
    var modal = $(this);
    modal.find("#me").text(r);
})

and this is my modal, I want when I open the modal to be able to capture the data-id and display it in id="me"

<div class="modal-body" id="modal-body">
                    <div class="row m-0">
                        <div class="col-4">
                            People name & twitter
                        </div>
                        <div class="col-8">
                            Descriptions
                            <h2 id="me"></h2>
                        </div>
                    </div>
                </div>
1
  • Hi, did you resolve this or still looking for answer ? Commented Oct 23, 2021 at 4:27

1 Answer 1

1

You can access data-... attribute with dataset property, e.g. given

<div id="my-element" data-id="myData"></div>

you can get it with

document.getElementById("my-element").dataset.id
Sign up to request clarification or add additional context in comments.

1 Comment

that did not work because the link tag is in js and I want when I open the modal to be able to send the data-id of that link into the h2 id="me"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.