-2

im trying to make a button that once click will make it so that the same javascript code would apply to the same class but it wont work

function Destination() {
  hrs = Math.round(Math.random() * 12);
  mins = Math.round(Math.random() * 60);
  var hFormat = (hrs < 10 ? "0" : "");
  var mFormat = (mins < 10 ? "0" : "");
  var amPm = (hrs < 12 ? "AM" : "PM");
  return String(hFormat + hrs + ":" + mFormat + mins + " " + amPm);
}
<button onclick="document.getElementsByClassName('AirAsiaTable1').innerHTML =Destination(12,60)">
  <i class="fas fa-search"></i>
</button>
Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101

1 Answers1

-1

getElementsByClassName returns an array of elements (if there are any). You need to get the first one. Referenced by index 0.

Try This CodePen

function Destination() {
  hrs = Math.round(Math.random() * 12);
  mins = Math.round(Math.random() * 60);
  var hFormat = (hrs < 10 ? "0" : "");
  var mFormat = (mins < 10 ? "0" : "");
  var amPm = (hrs < 12 ? "AM" : "PM");
  return String(hFormat + hrs + ":" + mFormat + mins + " " + amPm);
}
function btnClick(){
  var p = document.getElementsByClassName('AirAsiaTable1')[0]
  console.log(p)
  p.innerHTML =Destination(12,60)
}

https://codepen.io/ianmiddelkamp/pen/MWYGWeK?editors=1111