Home > Enterprise >  How to display multiple object values in alert box using javascript
How to display multiple object values in alert box using javascript

Time:09-04

function myFunction()
{
    alert(document.getElementById("myname").value ',' document.getElementById("myphone") ',' document.getElementById("mycountry") ',' document.getElementById("myemail"));
   
}

CodePudding user response:

it seems pretty straightforward to me

I just refactor a bit your function

function myFunction() {
  const message = ['myname', 'myphone', 'mycountry', 'myemail']
    .map(id => document.getElementById(id).value)
    .join(',')
  alert(message);

}
<div>
  <input id="myname" placeholder="myname" />
  <input id="myphone" placeholder="myphone" />
  <input id="mycountry" placeholder="mycountry" />
  <input id="myemail" placeholder="myemail" />
</div>
<div>
<button onclick="myFunction()">Show alert</button>
</div>

  • Related