Open the same modal with several buttons

1

I am retrieving the data from the mysql database, through a while, for each row of the database a button is generated, each button opens a modal window with details also coming from the database, but only the first button is opening the modal window!

html:

<?php  while ($row = $QUESTResult->fetch_array()) { ?>
<a id="test" class="active step"><?php echo $row['title']; ?></a>
<?php } ?>

<div class="ui modal test">
  <i class="close icon"></i>
  <div class="header">
    Profile Picture
  </div>
  <div class="image content">
    <div class="ui medium image">
      <img src="https://semantic-ui.com/images/avatar2/large/rachel.png"></div><divclass="description">
      <div class="ui header">We've auto-chosen a profile image for you.</div>
      <p>We've grabbed the following image from the <a href="https://www.gravatar.com" target="_blank">gravatar</a> image associated with your registered e-mail address.</p>
      <p>Is it okay to use this photo?</p>
    </div>
  </div>
  <div class="actions">
    <div class="ui black deny button">
      Nope
    </div>
    <div class="ui positive right labeled icon button">
      Yep, that's me
      <i class="checkmark icon"></i>
    </div>
  </div>
</div>

javascript:

<script type="text/javascript">
$(document).ready(function(){
        $("#test").click(function(){
            $(".test").modal('show');
        });
        $(".test").modal({
            closable: true
        });
});
</script>

NOTE: I am using Semantic-ui Framework

    
asked by anonymous 31.12.2017 / 21:51

1 answer

1

The problem is that you are using ID , which must be unique, in multiple elements.

The correct one is:

HTML:

<?php  while ($row = $QUESTResult->fetch_array()) { ?>
    <a id="test<?php echo $row["id"] ?>" class="active step"><?php echo $row['title']; ?></a>
<?php } ?>
  

You need to generate different ID's, eg test1, test2, test3, test4, etc.

JS:

$("a[id^=\"test\"]").click(function(){
    $(".test").modal('show');
});
    
31.12.2017 / 22:02