0
 <?php while($row = mysqli_fetch_array($result)): ?>
  <tr>
    <td><?php echo $row['mapid'] ?></td>
    <td><button id="m" type="button" value="<?php echo $row['map'] ?>" onclick="ready()">ddd</button></td>
  </tr>
  <?php endwhile;?>
  </table>

 

  <iframe id="k" src="" width="600" height="450" frameborder="0" style="border:0;" allowfullscreen="" aria-hidden="false" tabindex="0"></iframe>  
  
  <script>
  function ready() {
    var x = document.getElementById('m').getAttribute('value');
    var y =  "https://www.google.com/maps/embed?pb=";
    var z = y + x;
    alert(z);
    document.getElementById("k").src = z;
  }
</script>           

I do click button to open google map it work fine but it getting value from the first row because id is the same please help

Hect blunk
  • 15
  • 4
  • 1
    Yes, IDs must be unique within a document, that’s why `document.getElementById('m')` _correctly_ returns the first button. Inline event handlers like `onclick` are [not recommended](https://stackoverflow.com/q/11737873/4642212). They are an [obsolete, hard-to-maintain and unintuitive](https://stackoverflow.com/a/43459991/4642212) way of registering events. Always [use `addEventListener`](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#Inline_event_handlers_—_dont_use_these) or jQuery’s [`.on`](https://api.jquery.com/on/) instead. – Sebastian Simon Dec 28 '20 at 09:02
  • Use [event delegation](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Events#Event_delegation) instead of assigning multiple events — it’s more maintainable, and applies to dynamically added elements. E.g., use an [event argument](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#The_event_listener_callback)’s [`target`](https://developer.mozilla.org/en-US/docs/Web/API/Event/target). See [the tag info](https://stackoverflow.com/tags/event-delegation/info) and [What is DOM Event delegation?](https://stackoverflow.com/q/1687296/4642212). – Sebastian Simon Dec 28 '20 at 09:02

1 Answers1

-1

You can use this context inside ready().

 <?php while($row = mysqli_fetch_array($result)): ?>
  <tr>
    <td><?php echo $row['mapid'] ?></td>
    <td><button type="button" value="<?php echo $row['map'] ?>" onclick="ready.call(this)">ddd</button></td>
  </tr>
  <?php endwhile;?>
  </table>

 

  <iframe id="k" src="" width="600" height="450" frameborder="0" style="border:0;" allowfullscreen="" aria-hidden="false" tabindex="0"></iframe>  
  
  <script>
  var ready = function() {
    var y =  "https://www.google.com/maps/embed?pb=";
    var z = y + this.value;
    alert(z);
    document.getElementById("k").src = z;
  }
</script>   
Prime
  • 2,544
  • 1
  • 3
  • 19