Reputation: 27
I've looped through each object and passed each object's data to it's own div along with a button, in each div, that opens a modal window when clicked and displays the data for only that particular object.
However, my problem is, when i test each button to see if it's data displays I would see that the last object's data is displayed for all of the objects modal window. any ideas?
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Help</title>
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-
Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-
2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous">
</script>
<script
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
integrity="sha384-
JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
crossorigin="anonymous"></script>
<style>
.box{
width:400px;
height:800px;
background-color:grey;
}
.box2{
width:400px;
height:100px;
background-color:white;
}
</style>
</head>
<body>
<div id="box" class="box"></div>
<script>
var radio = [
{
name:"106 power",
address:"1620 sw 11th st",
phone:"305-892-9927"
},
{
name:"99 jamz",
address:"1900 sw 19th st",
phone:"305-892-9900"
}
]
for (i = 0; i < radio.length; i++){
var qb = radio[i].name;
document.getElementById('box').innerHTML += "<div class='box2'>" + radio[i].name + "<br>" + radio[i].address + "<br> <button type='button' id='button' data-toggle='modal' data-target='#myModal'>" + "Click me!" + "</button></div>";
$(document).ready(function(){
$("#button").click(function(){
$("#main-content").html(qb);
});
});
};
</script>
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×
</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body " id="main-content">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data- dismiss="modal">Close
</button>
</div>
</div>
</div>
</div>
</body>
</html>
Upvotes: 1
Views: 3098
Reputation: 1476
The difference between an ID and a class is that an ID can be used to identify one element, whereas a class can be used to identify more than one.
So you should change id with class
Here is your code :
<button type='button' id='button' data-toggle='modal' data-target='#myModal'>" + "Click me!" + "</button>
Here is modified code:
<button type='button' class='button' data-toggle='modal' data-target='#myModal'>" + "Click me!" + "</button>
Also you have loop where you are trying to attach event on #button click which attach several event on #button and when you click on button it fire many time so if you want to avoid this many time fire you should attach event outside from loop.
for (i = 0; i < radio.length; i++){
var qb = radio[i].name;
document.getElementById('box').innerHTML += "<div class='box2'>" + radio[i].name + "<br>" + radio[i].address + "<br> <button type='button' id='button' data-toggle='modal' data-target='#myModal'>" + "Click me!" + "</button></div>";
}
$(document).ready(function(){
$("#button").click(function(){
$("#main-content").html(qb);
});
});
Also if you want to show radio.name in content when click button you should use data- attribute it is my advice. change your code with this:
for (i = 0; i < radio.length; i++){
var qb = radio[i].name;
document.getElementById('box').innerHTML += "<div class='box2'>" + radio[i].name + "<br>" + radio[i].address + "<br> <button type='button' class='button' data-toggle='modal' data-target='#myModal' data-radio-name='" + qb + "'>" + "Click me!" + "</button></div>";
}
$(document).ready(function(){
$(".button").click(function(){
$("#main-content").html($(this).data('radio-name'));
});
});
Store arbitrary data associated with the matched elements or return the value at the named data store for the first element in the set of matched elements.
Upvotes: 2