dynamic modals when i click on dynamic buttons Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!When to use self over $this?How do I detect a click outside an element?Trigger a button click with JavaScript on the Enter key in a text boxHow can I merge properties of two JavaScript objects dynamically?Event binding on dynamically created elements?<button> vs. <input type=“button” />. Which to use?How can I know which radio button is selected via jQuery?How to create an HTML button that acts like a link?How to decide when to use Node.js?Don't close the modal when click on content with React.s
What to do with someone that cheated their way though university and a PhD program?
SQL Server placement of master database files vs resource database files
How to translate "red flag" into Spanish?
What's parked in Mil Moscow helicopter plant?
How long can a nation maintain a technological edge over the rest of the world?
RIP Packet Format
Is there an efficient way for synchronising audio events real-time with LEDs using an MCU?
Why is arima in R one time step off?
Putting Ant-Man on house arrest
What is ls Largest Number Formed by only moving two sticks in 508?
Is it OK if I do not take the receipt in Germany?
Are there existing rules/lore for MTG planeswalkers?
Determinant of a matrix with 2 equal rows
How do I deal with an erroneously large refund?
Marquee sign letters
Has a Nobel Peace laureate ever been accused of war crimes?
Is there a way to fake a method response using Mock or Stubs?
Is it appropriate to mention a relatable company blog post when you're asked about the company?
How to begin with a paragraph in latex
What is /etc/mtab in Linux?
What is a 'Key' in computer science?
When does Bran Stark remember Jamie pushing him?
TV series episode where humans nuke aliens before decrypting their message that states they come in peace
In search of the origins of term censor, I hit a dead end stuck with the greek term, to censor, λογοκρίνω
dynamic modals when i click on dynamic buttons
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!When to use self over $this?How do I detect a click outside an element?Trigger a button click with JavaScript on the Enter key in a text boxHow can I merge properties of two JavaScript objects dynamically?Event binding on dynamically created elements?<button> vs. <input type=“button” />. Which to use?How can I know which radio button is selected via jQuery?How to create an HTML button that acts like a link?How to decide when to use Node.js?Don't close the modal when click on content with React.s
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I, i'm new here so i don't really know mutch, but i need to figure out this thing:
i have a php page with some dynamic buttons.
I want this dynamic buttons to open a modal with dynamic content but I can't figure out why it doesn't work.
As you can see from my test site here, if you click on the first button it will display the modal with correct content, but if you click on the second button (dinamically generatated) it doesn't display anyting.
here's my code, but it's more simple to understand trough the previous link;
<!------------------------------------------- ATTIVAZIONE MODAL--------------------------------->
<script>
// Get the modal
var modal = document.getElementById('myModal');
// Get the button that opens the modal
var btn = document.getElementById("<?php echo $mq_solarium; ?>");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("chiudi_dimensione")[0];
// When the user clicks the button, open the modal
btn.onclick = function()
modal.style.display = "flex";
// When the user clicks on <span> (x), close the modal
span.onclick = function()
modal.style.display = "none";
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event)
if (event.target == modal)
modal.style.display = "none";
</script>
<!-------------------------------------------FINE ATTIVAZIONE MODAL--------------------------------->
.plan-wrapper
border: 3px solid #73AD21;
<div class="hero-wrapper dimensioni">
<?php
//estraggo i valori per smart
$query=mysqli_query($con, " SELECT * FROM appartamenti INNER JOIN moduli ON appartamenti.id_planimetria = moduli.id_planimetria WHERE taglio='monolocale' AND visibile='1' ");
?>
<div id="myBtn"class="plan-wrapper">click me</div>
<div style="background-color:DodgerBlue;" id="myModal" class="lightbox_wrapper_dimensione">
<div id="close" class="chiudi_dimensione">X(close)</div>
some variables from php
</div>
</div>
<?php
}
}
else
// redirect user to another page
header( "Location: ../index.php" ); die;
?>
<!------------------------------------------- CHIUSURA PHP--------------------------------->
</div>
javascript php html dynamic modal-dialog
add a comment |
I, i'm new here so i don't really know mutch, but i need to figure out this thing:
i have a php page with some dynamic buttons.
I want this dynamic buttons to open a modal with dynamic content but I can't figure out why it doesn't work.
As you can see from my test site here, if you click on the first button it will display the modal with correct content, but if you click on the second button (dinamically generatated) it doesn't display anyting.
here's my code, but it's more simple to understand trough the previous link;
<!------------------------------------------- ATTIVAZIONE MODAL--------------------------------->
<script>
// Get the modal
var modal = document.getElementById('myModal');
// Get the button that opens the modal
var btn = document.getElementById("<?php echo $mq_solarium; ?>");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("chiudi_dimensione")[0];
// When the user clicks the button, open the modal
btn.onclick = function()
modal.style.display = "flex";
// When the user clicks on <span> (x), close the modal
span.onclick = function()
modal.style.display = "none";
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event)
if (event.target == modal)
modal.style.display = "none";
</script>
<!-------------------------------------------FINE ATTIVAZIONE MODAL--------------------------------->
.plan-wrapper
border: 3px solid #73AD21;
<div class="hero-wrapper dimensioni">
<?php
//estraggo i valori per smart
$query=mysqli_query($con, " SELECT * FROM appartamenti INNER JOIN moduli ON appartamenti.id_planimetria = moduli.id_planimetria WHERE taglio='monolocale' AND visibile='1' ");
?>
<div id="myBtn"class="plan-wrapper">click me</div>
<div style="background-color:DodgerBlue;" id="myModal" class="lightbox_wrapper_dimensione">
<div id="close" class="chiudi_dimensione">X(close)</div>
some variables from php
</div>
</div>
<?php
}
}
else
// redirect user to another page
header( "Location: ../index.php" ); die;
?>
<!------------------------------------------- CHIUSURA PHP--------------------------------->
</div>
javascript php html dynamic modal-dialog
add a comment |
I, i'm new here so i don't really know mutch, but i need to figure out this thing:
i have a php page with some dynamic buttons.
I want this dynamic buttons to open a modal with dynamic content but I can't figure out why it doesn't work.
As you can see from my test site here, if you click on the first button it will display the modal with correct content, but if you click on the second button (dinamically generatated) it doesn't display anyting.
here's my code, but it's more simple to understand trough the previous link;
<!------------------------------------------- ATTIVAZIONE MODAL--------------------------------->
<script>
// Get the modal
var modal = document.getElementById('myModal');
// Get the button that opens the modal
var btn = document.getElementById("<?php echo $mq_solarium; ?>");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("chiudi_dimensione")[0];
// When the user clicks the button, open the modal
btn.onclick = function()
modal.style.display = "flex";
// When the user clicks on <span> (x), close the modal
span.onclick = function()
modal.style.display = "none";
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event)
if (event.target == modal)
modal.style.display = "none";
</script>
<!-------------------------------------------FINE ATTIVAZIONE MODAL--------------------------------->
.plan-wrapper
border: 3px solid #73AD21;
<div class="hero-wrapper dimensioni">
<?php
//estraggo i valori per smart
$query=mysqli_query($con, " SELECT * FROM appartamenti INNER JOIN moduli ON appartamenti.id_planimetria = moduli.id_planimetria WHERE taglio='monolocale' AND visibile='1' ");
?>
<div id="myBtn"class="plan-wrapper">click me</div>
<div style="background-color:DodgerBlue;" id="myModal" class="lightbox_wrapper_dimensione">
<div id="close" class="chiudi_dimensione">X(close)</div>
some variables from php
</div>
</div>
<?php
}
}
else
// redirect user to another page
header( "Location: ../index.php" ); die;
?>
<!------------------------------------------- CHIUSURA PHP--------------------------------->
</div>
javascript php html dynamic modal-dialog
I, i'm new here so i don't really know mutch, but i need to figure out this thing:
i have a php page with some dynamic buttons.
I want this dynamic buttons to open a modal with dynamic content but I can't figure out why it doesn't work.
As you can see from my test site here, if you click on the first button it will display the modal with correct content, but if you click on the second button (dinamically generatated) it doesn't display anyting.
here's my code, but it's more simple to understand trough the previous link;
<!------------------------------------------- ATTIVAZIONE MODAL--------------------------------->
<script>
// Get the modal
var modal = document.getElementById('myModal');
// Get the button that opens the modal
var btn = document.getElementById("<?php echo $mq_solarium; ?>");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("chiudi_dimensione")[0];
// When the user clicks the button, open the modal
btn.onclick = function()
modal.style.display = "flex";
// When the user clicks on <span> (x), close the modal
span.onclick = function()
modal.style.display = "none";
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event)
if (event.target == modal)
modal.style.display = "none";
</script>
<!-------------------------------------------FINE ATTIVAZIONE MODAL--------------------------------->
.plan-wrapper
border: 3px solid #73AD21;
<div class="hero-wrapper dimensioni">
<?php
//estraggo i valori per smart
$query=mysqli_query($con, " SELECT * FROM appartamenti INNER JOIN moduli ON appartamenti.id_planimetria = moduli.id_planimetria WHERE taglio='monolocale' AND visibile='1' ");
?>
<div id="myBtn"class="plan-wrapper">click me</div>
<div style="background-color:DodgerBlue;" id="myModal" class="lightbox_wrapper_dimensione">
<div id="close" class="chiudi_dimensione">X(close)</div>
some variables from php
</div>
</div>
<?php
}
}
else
// redirect user to another page
header( "Location: ../index.php" ); die;
?>
<!------------------------------------------- CHIUSURA PHP--------------------------------->
</div>
<!------------------------------------------- ATTIVAZIONE MODAL--------------------------------->
<script>
// Get the modal
var modal = document.getElementById('myModal');
// Get the button that opens the modal
var btn = document.getElementById("<?php echo $mq_solarium; ?>");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("chiudi_dimensione")[0];
// When the user clicks the button, open the modal
btn.onclick = function()
modal.style.display = "flex";
// When the user clicks on <span> (x), close the modal
span.onclick = function()
modal.style.display = "none";
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event)
if (event.target == modal)
modal.style.display = "none";
</script>
<!-------------------------------------------FINE ATTIVAZIONE MODAL--------------------------------->
.plan-wrapper
border: 3px solid #73AD21;
<div class="hero-wrapper dimensioni">
<?php
//estraggo i valori per smart
$query=mysqli_query($con, " SELECT * FROM appartamenti INNER JOIN moduli ON appartamenti.id_planimetria = moduli.id_planimetria WHERE taglio='monolocale' AND visibile='1' ");
?>
<div id="myBtn"class="plan-wrapper">click me</div>
<div style="background-color:DodgerBlue;" id="myModal" class="lightbox_wrapper_dimensione">
<div id="close" class="chiudi_dimensione">X(close)</div>
some variables from php
</div>
</div>
<?php
}
}
else
// redirect user to another page
header( "Location: ../index.php" ); die;
?>
<!------------------------------------------- CHIUSURA PHP--------------------------------->
</div>
<!------------------------------------------- ATTIVAZIONE MODAL--------------------------------->
<script>
// Get the modal
var modal = document.getElementById('myModal');
// Get the button that opens the modal
var btn = document.getElementById("<?php echo $mq_solarium; ?>");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("chiudi_dimensione")[0];
// When the user clicks the button, open the modal
btn.onclick = function()
modal.style.display = "flex";
// When the user clicks on <span> (x), close the modal
span.onclick = function()
modal.style.display = "none";
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event)
if (event.target == modal)
modal.style.display = "none";
</script>
<!-------------------------------------------FINE ATTIVAZIONE MODAL--------------------------------->
.plan-wrapper
border: 3px solid #73AD21;
<div class="hero-wrapper dimensioni">
<?php
//estraggo i valori per smart
$query=mysqli_query($con, " SELECT * FROM appartamenti INNER JOIN moduli ON appartamenti.id_planimetria = moduli.id_planimetria WHERE taglio='monolocale' AND visibile='1' ");
?>
<div id="myBtn"class="plan-wrapper">click me</div>
<div style="background-color:DodgerBlue;" id="myModal" class="lightbox_wrapper_dimensione">
<div id="close" class="chiudi_dimensione">X(close)</div>
some variables from php
</div>
</div>
<?php
}
}
else
// redirect user to another page
header( "Location: ../index.php" ); die;
?>
<!------------------------------------------- CHIUSURA PHP--------------------------------->
</div>
javascript php html dynamic modal-dialog
javascript php html dynamic modal-dialog
asked Sep 17 '18 at 12:09
Stefano ZanettiStefano Zanetti
687
687
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Problem is in getElementById()
it can triger only 1 id at the time you should change this to some another logic of clicking and geting the modal data or foreach this script as well as data and generate different id values
So you can have only 1 myBtn
id on whole page you can make something like myBtn1
,myBtn2
, myBtn3
...
or<div class='myBtn' data-id='1'>...content...</div>
and use this data-id
to access modal you need
For example to show modal using jquery you can do like this
first change the id to class in each button and add data-id='number_of_item'
numbers should be different the same thing you should do with modals you should generate them with id='myModal_number_of_item'
in the same foreach! After you can use code bellow to open modal with query its easier for me but you can make it with pure javascript.
$('.myBtn').on('click', function()
var id = $(this).data('id');
$('#myModal_'+id).show();
)
Hi @olexander, I Have understand the problem. Can you suggest me what logic I have to use please? i'm really new to this and my boss gave me this complex job to do... another question: so i have to generate dynamic data-id?
– Stefano Zanetti
Sep 17 '18 at 12:22
you should have unique values for each button for accessing correct modal window so yes it should be generated dynamically.
– Oleksandr Pobuta
Sep 17 '18 at 12:27
It Worked Fine! many thanks!!!!!!!!
– Stefano Zanetti
Sep 17 '18 at 14:51
@Stefano Zanetti you did it by yourself i just wanted to point you in the right direction, have fun there :)
– Oleksandr Pobuta
Sep 17 '18 at 14:52
add a comment |
In this case, you have id="myBtn"
in multiple areas. The ID is designed to be unique in an entire document.
So, when you have document.getElementById("myBtn")
and the event to that object, it will work only for the very first element as you experienced before.
The solution is either to have different unique IDs or just use className.
If you want with Vanilla Javascript this is the solution:
var elements = document.getElementsByClassName("<?php echo $mq_solarium; ?>");
for( let i = 0; i < elements.length; i ++)
elements[i].addEventListener("click", function()
modal.style.display = "flex";
);
Or using Jquery
$(".<?php echo $mq_solarium; ?>").click(function()
modal.style.display = "flex";
);
But make sure you will replace id="myBtn"
to class="myBtn"
Hi Harry, I think i'm understanding now. Now, if you check my link, you will see that the modals are all with differents and unique IDs, but now any of the buttons (i've changed the entire dynamic area to be the button) doesn't work.. i'm getting mad... :(
– Stefano Zanetti
Sep 17 '18 at 12:54
1
You have your code before jquery is defined. so instead of$(".myBtn").click(function() modal.style.display = "flex"; );
use$(document).ready(function()$(".myBtn").click(function() modal.style.display = "flex"; ););
p.s Please make sure that your jquery is included before your code
– harry
Sep 17 '18 at 13:00
1
just put this line<script src="https://code.jquery.com/jquery-3.3.1.min.js" type="text/javascript" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
somewhere at the top of you another scripts
– Oleksandr Pobuta
Sep 17 '18 at 13:10
Thank you! it worked! But i've another problem... when I click it appear the modal, but not with the correct information.. it display always the contents of the last button (you can see it if you compare the "price".. Can you help me for the last time? Thank you :(
– Stefano Zanetti
Sep 17 '18 at 13:29
@Stefano Zanetti for this you should specify modal you are trying to access, try using my code withdata-id
and take thisid
from the table or provide more php code how are you getting data after query
– Oleksandr Pobuta
Sep 17 '18 at 13:39
|
show 2 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52367432%2fdynamic-modals-when-i-click-on-dynamic-buttons%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Problem is in getElementById()
it can triger only 1 id at the time you should change this to some another logic of clicking and geting the modal data or foreach this script as well as data and generate different id values
So you can have only 1 myBtn
id on whole page you can make something like myBtn1
,myBtn2
, myBtn3
...
or<div class='myBtn' data-id='1'>...content...</div>
and use this data-id
to access modal you need
For example to show modal using jquery you can do like this
first change the id to class in each button and add data-id='number_of_item'
numbers should be different the same thing you should do with modals you should generate them with id='myModal_number_of_item'
in the same foreach! After you can use code bellow to open modal with query its easier for me but you can make it with pure javascript.
$('.myBtn').on('click', function()
var id = $(this).data('id');
$('#myModal_'+id).show();
)
Hi @olexander, I Have understand the problem. Can you suggest me what logic I have to use please? i'm really new to this and my boss gave me this complex job to do... another question: so i have to generate dynamic data-id?
– Stefano Zanetti
Sep 17 '18 at 12:22
you should have unique values for each button for accessing correct modal window so yes it should be generated dynamically.
– Oleksandr Pobuta
Sep 17 '18 at 12:27
It Worked Fine! many thanks!!!!!!!!
– Stefano Zanetti
Sep 17 '18 at 14:51
@Stefano Zanetti you did it by yourself i just wanted to point you in the right direction, have fun there :)
– Oleksandr Pobuta
Sep 17 '18 at 14:52
add a comment |
Problem is in getElementById()
it can triger only 1 id at the time you should change this to some another logic of clicking and geting the modal data or foreach this script as well as data and generate different id values
So you can have only 1 myBtn
id on whole page you can make something like myBtn1
,myBtn2
, myBtn3
...
or<div class='myBtn' data-id='1'>...content...</div>
and use this data-id
to access modal you need
For example to show modal using jquery you can do like this
first change the id to class in each button and add data-id='number_of_item'
numbers should be different the same thing you should do with modals you should generate them with id='myModal_number_of_item'
in the same foreach! After you can use code bellow to open modal with query its easier for me but you can make it with pure javascript.
$('.myBtn').on('click', function()
var id = $(this).data('id');
$('#myModal_'+id).show();
)
Hi @olexander, I Have understand the problem. Can you suggest me what logic I have to use please? i'm really new to this and my boss gave me this complex job to do... another question: so i have to generate dynamic data-id?
– Stefano Zanetti
Sep 17 '18 at 12:22
you should have unique values for each button for accessing correct modal window so yes it should be generated dynamically.
– Oleksandr Pobuta
Sep 17 '18 at 12:27
It Worked Fine! many thanks!!!!!!!!
– Stefano Zanetti
Sep 17 '18 at 14:51
@Stefano Zanetti you did it by yourself i just wanted to point you in the right direction, have fun there :)
– Oleksandr Pobuta
Sep 17 '18 at 14:52
add a comment |
Problem is in getElementById()
it can triger only 1 id at the time you should change this to some another logic of clicking and geting the modal data or foreach this script as well as data and generate different id values
So you can have only 1 myBtn
id on whole page you can make something like myBtn1
,myBtn2
, myBtn3
...
or<div class='myBtn' data-id='1'>...content...</div>
and use this data-id
to access modal you need
For example to show modal using jquery you can do like this
first change the id to class in each button and add data-id='number_of_item'
numbers should be different the same thing you should do with modals you should generate them with id='myModal_number_of_item'
in the same foreach! After you can use code bellow to open modal with query its easier for me but you can make it with pure javascript.
$('.myBtn').on('click', function()
var id = $(this).data('id');
$('#myModal_'+id).show();
)
Problem is in getElementById()
it can triger only 1 id at the time you should change this to some another logic of clicking and geting the modal data or foreach this script as well as data and generate different id values
So you can have only 1 myBtn
id on whole page you can make something like myBtn1
,myBtn2
, myBtn3
...
or<div class='myBtn' data-id='1'>...content...</div>
and use this data-id
to access modal you need
For example to show modal using jquery you can do like this
first change the id to class in each button and add data-id='number_of_item'
numbers should be different the same thing you should do with modals you should generate them with id='myModal_number_of_item'
in the same foreach! After you can use code bellow to open modal with query its easier for me but you can make it with pure javascript.
$('.myBtn').on('click', function()
var id = $(this).data('id');
$('#myModal_'+id).show();
)
edited Mar 22 at 14:54
answered Sep 17 '18 at 12:19
Oleksandr PobutaOleksandr Pobuta
324310
324310
Hi @olexander, I Have understand the problem. Can you suggest me what logic I have to use please? i'm really new to this and my boss gave me this complex job to do... another question: so i have to generate dynamic data-id?
– Stefano Zanetti
Sep 17 '18 at 12:22
you should have unique values for each button for accessing correct modal window so yes it should be generated dynamically.
– Oleksandr Pobuta
Sep 17 '18 at 12:27
It Worked Fine! many thanks!!!!!!!!
– Stefano Zanetti
Sep 17 '18 at 14:51
@Stefano Zanetti you did it by yourself i just wanted to point you in the right direction, have fun there :)
– Oleksandr Pobuta
Sep 17 '18 at 14:52
add a comment |
Hi @olexander, I Have understand the problem. Can you suggest me what logic I have to use please? i'm really new to this and my boss gave me this complex job to do... another question: so i have to generate dynamic data-id?
– Stefano Zanetti
Sep 17 '18 at 12:22
you should have unique values for each button for accessing correct modal window so yes it should be generated dynamically.
– Oleksandr Pobuta
Sep 17 '18 at 12:27
It Worked Fine! many thanks!!!!!!!!
– Stefano Zanetti
Sep 17 '18 at 14:51
@Stefano Zanetti you did it by yourself i just wanted to point you in the right direction, have fun there :)
– Oleksandr Pobuta
Sep 17 '18 at 14:52
Hi @olexander, I Have understand the problem. Can you suggest me what logic I have to use please? i'm really new to this and my boss gave me this complex job to do... another question: so i have to generate dynamic data-id?
– Stefano Zanetti
Sep 17 '18 at 12:22
Hi @olexander, I Have understand the problem. Can you suggest me what logic I have to use please? i'm really new to this and my boss gave me this complex job to do... another question: so i have to generate dynamic data-id?
– Stefano Zanetti
Sep 17 '18 at 12:22
you should have unique values for each button for accessing correct modal window so yes it should be generated dynamically.
– Oleksandr Pobuta
Sep 17 '18 at 12:27
you should have unique values for each button for accessing correct modal window so yes it should be generated dynamically.
– Oleksandr Pobuta
Sep 17 '18 at 12:27
It Worked Fine! many thanks!!!!!!!!
– Stefano Zanetti
Sep 17 '18 at 14:51
It Worked Fine! many thanks!!!!!!!!
– Stefano Zanetti
Sep 17 '18 at 14:51
@Stefano Zanetti you did it by yourself i just wanted to point you in the right direction, have fun there :)
– Oleksandr Pobuta
Sep 17 '18 at 14:52
@Stefano Zanetti you did it by yourself i just wanted to point you in the right direction, have fun there :)
– Oleksandr Pobuta
Sep 17 '18 at 14:52
add a comment |
In this case, you have id="myBtn"
in multiple areas. The ID is designed to be unique in an entire document.
So, when you have document.getElementById("myBtn")
and the event to that object, it will work only for the very first element as you experienced before.
The solution is either to have different unique IDs or just use className.
If you want with Vanilla Javascript this is the solution:
var elements = document.getElementsByClassName("<?php echo $mq_solarium; ?>");
for( let i = 0; i < elements.length; i ++)
elements[i].addEventListener("click", function()
modal.style.display = "flex";
);
Or using Jquery
$(".<?php echo $mq_solarium; ?>").click(function()
modal.style.display = "flex";
);
But make sure you will replace id="myBtn"
to class="myBtn"
Hi Harry, I think i'm understanding now. Now, if you check my link, you will see that the modals are all with differents and unique IDs, but now any of the buttons (i've changed the entire dynamic area to be the button) doesn't work.. i'm getting mad... :(
– Stefano Zanetti
Sep 17 '18 at 12:54
1
You have your code before jquery is defined. so instead of$(".myBtn").click(function() modal.style.display = "flex"; );
use$(document).ready(function()$(".myBtn").click(function() modal.style.display = "flex"; ););
p.s Please make sure that your jquery is included before your code
– harry
Sep 17 '18 at 13:00
1
just put this line<script src="https://code.jquery.com/jquery-3.3.1.min.js" type="text/javascript" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
somewhere at the top of you another scripts
– Oleksandr Pobuta
Sep 17 '18 at 13:10
Thank you! it worked! But i've another problem... when I click it appear the modal, but not with the correct information.. it display always the contents of the last button (you can see it if you compare the "price".. Can you help me for the last time? Thank you :(
– Stefano Zanetti
Sep 17 '18 at 13:29
@Stefano Zanetti for this you should specify modal you are trying to access, try using my code withdata-id
and take thisid
from the table or provide more php code how are you getting data after query
– Oleksandr Pobuta
Sep 17 '18 at 13:39
|
show 2 more comments
In this case, you have id="myBtn"
in multiple areas. The ID is designed to be unique in an entire document.
So, when you have document.getElementById("myBtn")
and the event to that object, it will work only for the very first element as you experienced before.
The solution is either to have different unique IDs or just use className.
If you want with Vanilla Javascript this is the solution:
var elements = document.getElementsByClassName("<?php echo $mq_solarium; ?>");
for( let i = 0; i < elements.length; i ++)
elements[i].addEventListener("click", function()
modal.style.display = "flex";
);
Or using Jquery
$(".<?php echo $mq_solarium; ?>").click(function()
modal.style.display = "flex";
);
But make sure you will replace id="myBtn"
to class="myBtn"
Hi Harry, I think i'm understanding now. Now, if you check my link, you will see that the modals are all with differents and unique IDs, but now any of the buttons (i've changed the entire dynamic area to be the button) doesn't work.. i'm getting mad... :(
– Stefano Zanetti
Sep 17 '18 at 12:54
1
You have your code before jquery is defined. so instead of$(".myBtn").click(function() modal.style.display = "flex"; );
use$(document).ready(function()$(".myBtn").click(function() modal.style.display = "flex"; ););
p.s Please make sure that your jquery is included before your code
– harry
Sep 17 '18 at 13:00
1
just put this line<script src="https://code.jquery.com/jquery-3.3.1.min.js" type="text/javascript" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
somewhere at the top of you another scripts
– Oleksandr Pobuta
Sep 17 '18 at 13:10
Thank you! it worked! But i've another problem... when I click it appear the modal, but not with the correct information.. it display always the contents of the last button (you can see it if you compare the "price".. Can you help me for the last time? Thank you :(
– Stefano Zanetti
Sep 17 '18 at 13:29
@Stefano Zanetti for this you should specify modal you are trying to access, try using my code withdata-id
and take thisid
from the table or provide more php code how are you getting data after query
– Oleksandr Pobuta
Sep 17 '18 at 13:39
|
show 2 more comments
In this case, you have id="myBtn"
in multiple areas. The ID is designed to be unique in an entire document.
So, when you have document.getElementById("myBtn")
and the event to that object, it will work only for the very first element as you experienced before.
The solution is either to have different unique IDs or just use className.
If you want with Vanilla Javascript this is the solution:
var elements = document.getElementsByClassName("<?php echo $mq_solarium; ?>");
for( let i = 0; i < elements.length; i ++)
elements[i].addEventListener("click", function()
modal.style.display = "flex";
);
Or using Jquery
$(".<?php echo $mq_solarium; ?>").click(function()
modal.style.display = "flex";
);
But make sure you will replace id="myBtn"
to class="myBtn"
In this case, you have id="myBtn"
in multiple areas. The ID is designed to be unique in an entire document.
So, when you have document.getElementById("myBtn")
and the event to that object, it will work only for the very first element as you experienced before.
The solution is either to have different unique IDs or just use className.
If you want with Vanilla Javascript this is the solution:
var elements = document.getElementsByClassName("<?php echo $mq_solarium; ?>");
for( let i = 0; i < elements.length; i ++)
elements[i].addEventListener("click", function()
modal.style.display = "flex";
);
Or using Jquery
$(".<?php echo $mq_solarium; ?>").click(function()
modal.style.display = "flex";
);
But make sure you will replace id="myBtn"
to class="myBtn"
answered Sep 17 '18 at 12:44
harryharry
838
838
Hi Harry, I think i'm understanding now. Now, if you check my link, you will see that the modals are all with differents and unique IDs, but now any of the buttons (i've changed the entire dynamic area to be the button) doesn't work.. i'm getting mad... :(
– Stefano Zanetti
Sep 17 '18 at 12:54
1
You have your code before jquery is defined. so instead of$(".myBtn").click(function() modal.style.display = "flex"; );
use$(document).ready(function()$(".myBtn").click(function() modal.style.display = "flex"; ););
p.s Please make sure that your jquery is included before your code
– harry
Sep 17 '18 at 13:00
1
just put this line<script src="https://code.jquery.com/jquery-3.3.1.min.js" type="text/javascript" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
somewhere at the top of you another scripts
– Oleksandr Pobuta
Sep 17 '18 at 13:10
Thank you! it worked! But i've another problem... when I click it appear the modal, but not with the correct information.. it display always the contents of the last button (you can see it if you compare the "price".. Can you help me for the last time? Thank you :(
– Stefano Zanetti
Sep 17 '18 at 13:29
@Stefano Zanetti for this you should specify modal you are trying to access, try using my code withdata-id
and take thisid
from the table or provide more php code how are you getting data after query
– Oleksandr Pobuta
Sep 17 '18 at 13:39
|
show 2 more comments
Hi Harry, I think i'm understanding now. Now, if you check my link, you will see that the modals are all with differents and unique IDs, but now any of the buttons (i've changed the entire dynamic area to be the button) doesn't work.. i'm getting mad... :(
– Stefano Zanetti
Sep 17 '18 at 12:54
1
You have your code before jquery is defined. so instead of$(".myBtn").click(function() modal.style.display = "flex"; );
use$(document).ready(function()$(".myBtn").click(function() modal.style.display = "flex"; ););
p.s Please make sure that your jquery is included before your code
– harry
Sep 17 '18 at 13:00
1
just put this line<script src="https://code.jquery.com/jquery-3.3.1.min.js" type="text/javascript" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
somewhere at the top of you another scripts
– Oleksandr Pobuta
Sep 17 '18 at 13:10
Thank you! it worked! But i've another problem... when I click it appear the modal, but not with the correct information.. it display always the contents of the last button (you can see it if you compare the "price".. Can you help me for the last time? Thank you :(
– Stefano Zanetti
Sep 17 '18 at 13:29
@Stefano Zanetti for this you should specify modal you are trying to access, try using my code withdata-id
and take thisid
from the table or provide more php code how are you getting data after query
– Oleksandr Pobuta
Sep 17 '18 at 13:39
Hi Harry, I think i'm understanding now. Now, if you check my link, you will see that the modals are all with differents and unique IDs, but now any of the buttons (i've changed the entire dynamic area to be the button) doesn't work.. i'm getting mad... :(
– Stefano Zanetti
Sep 17 '18 at 12:54
Hi Harry, I think i'm understanding now. Now, if you check my link, you will see that the modals are all with differents and unique IDs, but now any of the buttons (i've changed the entire dynamic area to be the button) doesn't work.. i'm getting mad... :(
– Stefano Zanetti
Sep 17 '18 at 12:54
1
1
You have your code before jquery is defined. so instead of
$(".myBtn").click(function() modal.style.display = "flex"; );
use $(document).ready(function()$(".myBtn").click(function() modal.style.display = "flex"; ););
p.s Please make sure that your jquery is included before your code– harry
Sep 17 '18 at 13:00
You have your code before jquery is defined. so instead of
$(".myBtn").click(function() modal.style.display = "flex"; );
use $(document).ready(function()$(".myBtn").click(function() modal.style.display = "flex"; ););
p.s Please make sure that your jquery is included before your code– harry
Sep 17 '18 at 13:00
1
1
just put this line
<script src="https://code.jquery.com/jquery-3.3.1.min.js" type="text/javascript" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
somewhere at the top of you another scripts– Oleksandr Pobuta
Sep 17 '18 at 13:10
just put this line
<script src="https://code.jquery.com/jquery-3.3.1.min.js" type="text/javascript" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
somewhere at the top of you another scripts– Oleksandr Pobuta
Sep 17 '18 at 13:10
Thank you! it worked! But i've another problem... when I click it appear the modal, but not with the correct information.. it display always the contents of the last button (you can see it if you compare the "price".. Can you help me for the last time? Thank you :(
– Stefano Zanetti
Sep 17 '18 at 13:29
Thank you! it worked! But i've another problem... when I click it appear the modal, but not with the correct information.. it display always the contents of the last button (you can see it if you compare the "price".. Can you help me for the last time? Thank you :(
– Stefano Zanetti
Sep 17 '18 at 13:29
@Stefano Zanetti for this you should specify modal you are trying to access, try using my code with
data-id
and take this id
from the table or provide more php code how are you getting data after query– Oleksandr Pobuta
Sep 17 '18 at 13:39
@Stefano Zanetti for this you should specify modal you are trying to access, try using my code with
data-id
and take this id
from the table or provide more php code how are you getting data after query– Oleksandr Pobuta
Sep 17 '18 at 13:39
|
show 2 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52367432%2fdynamic-modals-when-i-click-on-dynamic-buttons%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown